我的XSLT有什么错误?(What is the error in my XSLT?)

编程入门 行业动态 更新时间:2024-10-04 17:28:08
我的XSLT有什么错误?(What is the error in my XSLT?)

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

我的输出只是浏览器中的“The Shelf”。 我错在哪里?

This is my xml file

<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>

This is my XSL file

<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>

My output is just "The Shelf" in the browser. Where am I wrong?

最满意答案

你有两个问题。

你的数据有一个命名空间“http://www.w3schools.com”,但你不能只在xslt中声明并使用它 - 由于xml / xpath规范不匹配,你需要更改你的选择器。 我已经声明了一个'data'前缀来匹配你的xml文档的默认名字空间,然后改变你所有的xpath选择来匹配。 不幸的是,你不能只是一个默认的命名空间,因为默认的命名空间在xpath中不起作用。 (或者,您可能已经从xml文档中删除了默认名称空间,但这可能并不总是一个选项)。

您的货架选择器将找不到与'/'相关的任何匹配节点。 我已将您的初始应用模板更改为// 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>

You have two problems.

Your data has a namespace "http://www.w3schools.com", however you can't just declare this in your xslt and use it - due to a mismatch in the xml/xpath specs you need to change your selectors as well. I've declared a 'data' prefix to match your xml documents default name space, then changed all your xpath selects to match. Unfortunatly you can't just a default namespace, as default name spaces don't work in xpath. (alternatively, you could have removed the default namespace from your xml document, but this might not always be an option).

Your shelf selector won't find any matching nodes relative to '/'. I've change your initial apply templates to //data:shelf to match all data:shelf nodes that can be found anywhere in your document.

try the following

<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>

更多推荐

本文发布于:2023-08-07 13:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   错误   XSLT   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!