【问题标题】:XSLT transformation not showing any dataXSLT 转换未显示任何数据
【发布时间】:2015-01-10 18:43:53
【问题描述】:

我已经使用转换工具在 XML 和 XSL 下进行了测试。输出不显示任何数据行。谁能帮我找出问题所在?

我使用了下面的在线工具。

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex3

XML

<?xml version="1.0" encoding="UTF-8"?>
<vehicles xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3schools.comm/2001/XMLSchema-instance">
        <vehicle>
            <make>Toyota</make>
            <model>Prius</model>
            <color>White</color>
            <yearofmanufacture>2013</yearofmanufacture>
            <engine>1.5CC</engine>
            <doors>5</doors>
        </vehicle>      
</vehicles>

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>Car Sale - Stock List</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Make</th>
                    </tr>
                    <xsl:for-each select="vehicles/vehicle">
                        <tr>
                            <td>
                                <xsl:value-of select="make" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

输出:

汽车销售 - 库存清单

制作

【问题讨论】:

标签: html xml xslt xpath xml-namespaces


【解决方案1】:

是的,问题是您没有在 XPath 中使用命名空间,而您应该这样做:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ws="http://www.w3schools.com">
    <!--   ^---- here    -->
    <xsl:template match="/">
        <html>
            <body>
                <h2>Car Sale - Stock List</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Make</th>
                    </tr>
                    <!-- here ------------v -----------v   -->
                    <xsl:for-each select="ws:vehicles/ws:vehicle">
                        <tr>
                            <td>
                                <!-- and here --------v       -->
                                <xsl:value-of select="ws:make" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

  • @JSIK 如果它解决了您的问题,请不要忘记accept this answer。我确信这就是解决方案。谢谢!
猜你喜欢
  • 2021-11-24
  • 2015-11-09
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
相关资源
最近更新 更多