Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 我的XSLT中有什么错误?_Xml_Xslt_Xpath - Fatal编程技术网

Xml 我的XSLT中有什么错误?

Xml 我的XSLT中有什么错误?,xml,xslt,xpath,Xml,Xslt,Xpath,这是我的xml文件 <html xmlns="http://www.w3schools.com"> <body> <shelf> <cd id="el01"> <artist>Elton John</artist> <title>Circle of Life</title>

这是我的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>

埃尔顿·约翰
生命周期
英国
谱
10.90
1999
正如在《狮子王》中听到的那样。
马修·甘巴德拉
XML开发人员指南
计算机
44.95
2000-10-01
深入了解如何创建应用程序
使用XML。
这是我的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>

架子
架子

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

你的select=“shelf”错了。如果你把它取下来就行了。或者尝试select=“html/body/shelf”。

您的select=“shelf”错误。如果你把它取下来就行了。或者尝试select=“html/body/shelf”。

您的行
将应用于根节点(即html/shelf)的上下文中。此级别没有工具架节点

将该行更改为
就足够了。

您的行
将应用于根节点(即html/shelf)的上下文中。此级别没有工具架节点

将该行更改为
就足够了。

您有两个问题

  • 您的数据有一个名称空间“http://www.w3schools.com但是,您不能在xslt中声明并使用它,因为xml/xpath规范不匹配,您还需要更改选择器。我声明了一个“data”前缀以匹配xml文档的默认名称空间,然后将所有xpath选择更改为匹配。不幸的是,不能只使用默认名称空间,因为默认名称空间在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>
    
    
    架子
    架子
    

    您有两个问题

  • 您的数据有一个名称空间“http://www.w3schools.com但是,您不能在xslt中声明并使用它,因为xml/xpath规范不匹配,您还需要更改选择器。我声明了一个“data”前缀以匹配xml文档的默认名称空间,然后将所有xpath选择更改为匹配。不幸的是,不能只使用默认名称空间,因为默认名称空间在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>
    
    
    架子
    架子
    


    +1用于发现名称空间问题!。但是请注意,通常使用
    /
    快捷方式并不好,因为它会在所有级别查找所有匹配的节点,这在大型文档中可能效率低下。如果您知道实际路径,最好选择它。+1用于发现名称空间问题!。但是请注意,通常使用
    /
    快捷方式并不好,因为它会在所有级别查找所有匹配的节点,这在大型文档中可能效率低下。如果你知道的话,最好选择实际的路径。