首页 > 解决方案 > 如何通过 Xslt 转换转换 xml

问题描述

我有xml输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="/topic/get-all.xslt"?>
<List>
    <item>
        <id>8541915a-9098-4d10-bf80-5fbc9a5800af</id>
        <name>TestTopfdic4</name>
        <modifiedDate>2019-12-18T12:37:42.718</modifiedDate>
    </item>
    <item>
        <id>55bc34e6-5cd2-436a-9d37-ceb1052187b0</id>
        <name>TestTopfdic4</name>
        <modifiedDate>2019-12-18T12:40:12.948</modifiedDate>
    </item>
    <item>
        <id>2fee9ce3-1595-4c56-9833-cda03642ad05</id>
        <name>TestTopfdic4</name>
        <modifiedDate>2019-12-18T12:42:15.385</modifiedDate>
    </item>
</List>

并尝试通过 xslt 将 tarnsform 转换为 html,我尝试选择每个项目并单独处理它

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/*">
        <html>
            <body>
                <table align="center">
                    <thead>
                        <th>Id</th>
                        <th>Name</th>
                        <th>modified date</th>
                    </thead>
                    <tbody>
                        <xsl:for-each select="List">
                            <tr>
                                <td><xsl:value-of select="item.id"/></td>
                                <td><xsl:value-of select="item.name"/></td>
                                <td><xsl:value-of select="item.modifiedDate"/></td>
                            </tr>
                        </xsl:for-each>
                    </tbody>

                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

但是得到空输出,我做错了什么?你能帮我解决这个问题吗?

标签: xmlxslt

解决方案


您的语法不是 XPath 语法。尝试:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/List">
        <html>
            <body>
                <table align="center">
                    <thead>
                        <th>Id</th>
                        <th>Name</th>
                        <th>modified date</th>
                    </thead>
                    <tbody>
                        <xsl:for-each select="item">
                            <tr>
                                <td><xsl:value-of select="id"/></td>
                                <td><xsl:value-of select="name"/></td>
                                <td><xsl:value-of select="modifiedDate"/></td>
                            </tr>
                        </xsl:for-each>
                    </tbody>

                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

推荐阅读