xml - 我的 XSLT 中有什么错误?

标签 xml xslt xpath

这是我的 xml 文件

<html xmlns="http://www.w3schools.com">
    <body>
        <shelf>
            <cd id="el01">
                <artist>Elton John</artist>
                <title>Circle of Life</title>
                <country>UK</country>
                <company>Spectrum</company>
                <price>10.90</price>
                <year>1999</year>
                <description>As heard in the Lion King.</description>
            </cd>

            <book id="bk101">
                <author>Gambardella, Matthew</author>
                <title>XML Developer's Guide</title>
                <genre>Computer</genre>
                <price>44.95</price>
                <publish_date>2000-10-01</publish_date>
                <description>An in-depth look at creating applications 
      with XML.
                </description>
            </book>

          </shelf>
    </body>
</html>

这是我的 XSL 文件
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>

<!-- TODO customize transformation rules 
     syntax recommendation http://www.w3.org/TR/xslt 
-->
<xsl:template match="/">
    <html>
        <head>
            <title>The Shelf</title>
        </head>
        <body>
            <h1>The Shelf</h1>
            <xsl:apply-templates select="shelf"/>
        </body>
    </html>
</xsl:template>
<xsl:template match="//shelf">
    <xsl:for-each select="cd|book">
        <xsl:value-of select="title"/>
    </xsl:for-each>
</xsl:template>

我的输出只是浏览器中的“架子”。我哪里错了?

最佳答案

你有两个问题。

  • 您的数据有一个命名空间“http://www.w3schools.com”,但是您不能只在 xslt 中声明并使用它 - 由于 xml/xpath 规范不匹配,您还需要更改选择器.我已经声明了一个“数据”前缀来匹配您的 xml 文档默认 namespace ,然后更改所有 xpath 选择以匹配。不幸的是,您不能只使用默认 namespace ,因为默认 namespace 在 xpath 中不起作用。 (或者,您可以从 xml 文档中删除默认命名空间,但这可能并不总是一种选择)。
  • 您的架子选择器不会找到任何与“/”相关的匹配节点。我已将您的初始应用模板更改为//data:shelf 以匹配可以在文档中的任何位置找到的所有 data:shelf 节点。

  • 尝试以下
    <xsl:stylesheet xmlns:data="http://www.w3schools.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    
    <!-- TODO customize transformation rules
     syntax recommendation http://www.w3.org/TR/xslt
    -->
    <xsl:template match="/">
    <html>
        <head>
            <title>The Shelf</title>
        </head>
        <body>
            <h1>The Shelf</h1>
            <xsl:apply-templates select="//data:shelf"/>
        </body>
    </html>
    </xsl:template>
    <xsl:template match="//data:shelf">
    <xsl:for-each select="data:cd|data:book">
        <p><xsl:value-of select="data:title"/></p>
    </xsl:for-each>
    </xsl:template>
    
    </xsl:stylesheet>
    

    关于xml - 我的 XSLT 中有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7858336/

    相关文章:

    java - 使用 Java 读取包含特殊字符(&、- 等)的 XML 文档节点

    android - 如何通过XML启动Language和Input Intent?

    java - 使用现有的 ant build.xml 文件创建一个 eclipse 项目

    java - 将美国日期转换为日本日期

    javascript - 使用selenium使用xpath输入多个元素

    selenium - 在 Xpath 中使用 OR 条件来标识相同的元素

    android - coverflow 未显示在 Android 的 xml 布局上

    html - XML 中的 XSLT 动态列

    html - 在 xsl :text in roundrect tag in xslt 中使用变量

    javascript - 我可以使用 Xpath 作为 <td> 元素列表来获取名称列表而不是 javascript 吗?