XPath无法识别路径

时间:2017-12-12 10:20:10

标签: html xml xslt xpath

我正在编写一个XSL转换器来将XML转换为HTML。这是我的 gun.xml

<?xml version="1.0" encoding="utf-8"?>

<?xml-stylesheet type="text/xsl" href="guns.xslt"?>
<guns xsi:noNamespaceSchemaLocation="guns2.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <gun>
        <model>revolver</model>
        <handy>1</handy>
        <origin>Britain</origin>
        <ttc>20mm</ttc>
    </gun>
</guns>

guns.xslt 此处:

<?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>Gun Collection</h2>
                <table border = "1">
                    <tr bgcolor = "#9acd32">
                        <th>Model</th>
                        <th>Origin</th>
                        <th>TTC</th>
                    </tr>
                    <xsl:for-each select="guns/gun">
                        <tr>
                            <td><xsl:value-of select = "model"/></td>
                            <td><xsl:value-of select = "handy"/></td>
                            <td><xsl:value-of select = "origin"/></td>
                            <td><xsl:value-of select = "ttc"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

两个文件都位于同一个包中,但XPAth无法识别"guns/gun"块中的for-each。我错过了什么?

1 个答案:

答案 0 :(得分:2)

您已在xml-stylesheet中声明 - 键入“text / xsl”,请将文件扩展名从“guns.xslt”更改为“guns.xsl”,并更改XML,如下所示:

<?xml-stylesheet type="text/xsl" href="guns.xsl"?>

然后你的结果如下:

enter image description here