XSLT无法从XML读取数据

时间:2017-06-14 21:52:17

标签: xml xslt

我是XML和XSL的新手。使用XSL进行XML转换但不起作用。有关此主题的任何见解,为什么XSL不从XML打印数据?

下面是XML输入,XSL,当前输出和输出的原始示例。预期产出

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>

当前输出

Info

PersonID    Status  PersonName  ServiceName NoOfService
                                            /1
                                            /2
                                            /1

预期产出

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

1 个答案:

答案 0 :(得分:1)

Id的{​​{1}}是属性,而不是元素。你必须使用:

Person

选择它,而不是:

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

同样适用于其他细胞。