Hemant
Hemant

Reputation: 11

XSLT not able to read the data from XML

I am new to XML and XSL. Doing the transformations of XML using XSL but its not working. Any insight on this topic, why XSL is not printing the data from XML?

Below is a raw example of XML Input, XSL, Current output & Expected Output

XML

<PersonalDetails Target='prepare-html-output'>
    <MultiApi>
        <API Name="getPersonalDetails">
            <Output>
                <Person Id="ID_0" Name="Name1" Status="Active" Type="INT">
                    <Service Name="Service_0" NoOfService="3" Status="Active"/>
                </Person>
            </Output>
        </API>
        <API Name="getPersonalDetails">
            <Output>
                <Person Id="ID_1" Name="Name2" Status="Active" Type="INT">
                    <Service Name="Service_1" NoOfService="3" Status="Active"/>
                    <Service Name="Service_2" NoOfService="1" Status="Active"/>
                </Person>
            </Output>
        </API>
        <API Name="getPersonalDetails">
            <Output>
                <Person Id="ID_2" Name="Name3" Status="Active" Type="INT">
                    <Service Name="Service_3" NoOfService="2" Status="Active"/>
                </Person>
            </Output>
        </API>
    </MultiApi>
</PersonalDetails>

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html> 
            <body>
                <h2>Info </h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th style="text-align:left">PersonID</th>
                        <th style="text-align:left">Status</th>
                        <th style="text-align:left">PersonName</th>
                        <th style="text-align:left">ServiceName</th>
                        <th style="text-align:left">NoOfService</th>
                    </tr>
                    <xsl:for-each select="PersonalDetails/MultiApi/API/Output">
                        <tr>
                            <td><xsl:value-of select="Person/Id"/></td>
                            <td><xsl:value-of select="Person/Status"/></td>
                            <td><xsl:value-of select="Person/Name"/></td>
                            <td><xsl:value-of select="Person/Service/Name"/></td>
                            <td><xsl:value-of select="Person/Service/NoOfService"/>
                                <xsl:text>/</xsl:text>
                                <xsl:value-of select="count(Person[@Id]/Service[@Name])"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Current Output

Info

PersonID    Status  PersonName  ServiceName NoOfService
                                            /1
                                            /2
                                            /1

Expected Output

Info

PersonID    Status  PersonName  ServiceName NoOfService
ID_0        Active  Name1       Service_0   3/1
ID_1        Active  Name2       Service_1   3/1
ID_1        Active  Name2       Service_2   1/1
ID_2        Active  Name3       Service_3   2/1

Upvotes: 1

Views: 428

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

The Id of a Person is an attribute, not an element. You must use:

<xsl:value-of select="Person/@Id"/>

to select it, instead of:

<xsl:value-of select="Person/Id"/>

Likewise for your other cells.

Upvotes: 1

Related Questions