【问题标题】:XPath doesn't recognize pathXPath 无法识别路径
【发布时间】:2018-05-25 23:50:24
【问题描述】:

我正在编写一个 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 无法识别 for-each 块中的 "guns/gun"。我错过了什么?

【问题讨论】:

    标签: html xml xslt xpath


    【解决方案1】:

    您已在 xml-stylesheet 中声明 - 类型为“text/xsl”,请将文件扩展名从“guns.xslt”更改为“guns.xsl”,并在您的 XML 中进行如下更改:

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

    那么你的结果如下:

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 2016-09-11
      • 2015-04-19
      • 2019-07-20
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多