XSLT 转换不显示任何数据

XSLT transformation not showing any data

我已经在下面 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>

输出:

Car Sale - Stock List

Make

是的,问题是您没有在 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>