XSL:中的 Xpath 查询不返回任何结果
注 1:这是我的第一个问题。请温柔点。
注2:是的,这是作业。我讨厌寻求帮助,但我已经碰壁了。如果您不想告诉我答案,建议在哪里寻找答案也将同样受到赞赏。谢谢。
我正在尝试编写一个 XSLT 表来查看目标 XML 文件中每个 DVD 元素的流派属性。它应该返回一个列表,其中每个不同的值仅列出一次,并统计每个值被发现的次数。
我认为我的 XPath 是错误的。为了调试,我尝试使用
语句来尝试不同的 XPath 查询,但我没有任何运气。
这是 XSL 文件(我已尝试将其简化)
<?xml version="1.0" encoding="UTF-8"?>
<!-- BrowseAllExample.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="genres" match="DVD" use="@genre" />
<xsl:template match="/">
<html>
<head>
<title>Browse: DVDs by Genre</title>
<link href="../style/browseByGenre.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- TODO: insert page header here -->
<h2>Browse by Genre</h2>
<div>
<!-- This doesn't seem to select anything
I've tried lots of combinations with a test
xsl:value-of-select element, but nothing
seems to work as a selector except "*"
-->
<xsl:apply-templates select="/dvdlist/DVD[not(@genre=preceding-sibling/@genre)]"
mode="genreList">
<xsl:sort select="@genre"/>
</xsl:apply-templates>
</div>
</body>
</html>
</xsl:template>
<!--
List each genre and count titles in each
-->
<xsl:template match="DVD" mode="genreList">
<a href="#{generate-id()}"><xsl:value-of select="@genre"/></a> (<xsl:value-of
select="count(key('genres',@genre))"/>) -
</xsl:template>
</xsl:stylesheet>
这是 XML 文件的相关部分:
<?xml version="1.0" encoding="UTF-8"?>
<!-- dvdlistExample.xml -->
<?xml-stylesheet type="text/xsl" href="BrowseAllExample.xsl"?>
<dvdlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/dvdlist ./schema/dvdlist.xsd"
xmlns="http://example.com/dvdlist">
<DVD genre="Comedy" itemnum="ID-20">
<title>American Wedding</title>
<year>2003</year>
<price>18.89</price>
<talent>
<director>Jesse Dylan</director>
<actor>Jason Biggs</actor>
<actor>Sean William Scott</actor>
<actor/>
</talent>
<ratings mpaa="NR" customer="3"/>
<pics id="ID-20">
<thumbnail>Wedding-small.jpg</thumbnail>
<large>Wedding-big.jpg</large>
</pics>
</DVD>
<DVD genre="Action" itemnum="ID-2">
<title>Badboys II</title>
<year>2003</year>
<price>20.27</price>
<talent>
<director>Michael Bay</director>
<actor>Will Smith</actor>
<actor>Martin Lawrence</actor>
</talent>
<ratings mpaa="R" customer="3"/>
<pics id="ID-2">
<thumbnail>Badboys-small.jpg</thumbnail>
<large>Badboys-big.jpg</large>
</pics>
</DVD>
<!--Other DVD entries removed to save space-->
</dvdlist>
Note 1: This is my first question here. Please be gentle.
Note 2: Yes, this is homework. I hate to ask for help, but I've hit a brick wall. If you don't want to tell me the answer, suggesting where to look for the answer would be equally appreciated. Thank you.
I'm trying to write an XSLT sheet that looks at the genre attribute of each DVD element in the target XML file. It is supposed to return a list with each of the different values listed exactly once with a count of the number of times each value was found.
I think my XPath is wrong. To debug, I've tried using <xsl:value-of select="foo">
statements to try different XPath queries, but I haven't had any luck.
Here's the XSL file (I've tried to strip it to the brass tacks)
<?xml version="1.0" encoding="UTF-8"?>
<!-- BrowseAllExample.xsl -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="genres" match="DVD" use="@genre" />
<xsl:template match="/">
<html>
<head>
<title>Browse: DVDs by Genre</title>
<link href="../style/browseByGenre.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- TODO: insert page header here -->
<h2>Browse by Genre</h2>
<div>
<!-- This doesn't seem to select anything
I've tried lots of combinations with a test
xsl:value-of-select element, but nothing
seems to work as a selector except "*"
-->
<xsl:apply-templates select="/dvdlist/DVD[not(@genre=preceding-sibling/@genre)]"
mode="genreList">
<xsl:sort select="@genre"/>
</xsl:apply-templates>
</div>
</body>
</html>
</xsl:template>
<!--
List each genre and count titles in each
-->
<xsl:template match="DVD" mode="genreList">
<a href="#{generate-id()}"><xsl:value-of select="@genre"/></a> (<xsl:value-of
select="count(key('genres',@genre))"/>) -
</xsl:template>
</xsl:stylesheet>
Here's the relevant part of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- dvdlistExample.xml -->
<?xml-stylesheet type="text/xsl" href="BrowseAllExample.xsl"?>
<dvdlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/dvdlist ./schema/dvdlist.xsd"
xmlns="http://example.com/dvdlist">
<DVD genre="Comedy" itemnum="ID-20">
<title>American Wedding</title>
<year>2003</year>
<price>18.89</price>
<talent>
<director>Jesse Dylan</director>
<actor>Jason Biggs</actor>
<actor>Sean William Scott</actor>
<actor/>
</talent>
<ratings mpaa="NR" customer="3"/>
<pics id="ID-20">
<thumbnail>Wedding-small.jpg</thumbnail>
<large>Wedding-big.jpg</large>
</pics>
</DVD>
<DVD genre="Action" itemnum="ID-2">
<title>Badboys II</title>
<year>2003</year>
<price>20.27</price>
<talent>
<director>Michael Bay</director>
<actor>Will Smith</actor>
<actor>Martin Lawrence</actor>
</talent>
<ratings mpaa="R" customer="3"/>
<pics id="ID-2">
<thumbnail>Badboys-small.jpg</thumbnail>
<large>Badboys-big.jpg</large>
</pics>
</DVD>
<!--Other DVD entries removed to save space-->
</dvdlist>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用命名空间从 XML 中“选择”?应该可以帮助您满足您的需要。
XML 命名空间只是一种实现 XML 元素和属性唯一性的方法。如果两个用户/公司/无论什么都定义了
元素,则命名空间就是它们保持分离的方式。例如,您可以通过选择每个元素以及定义该元素的相应命名空间来处理两个不同的
元素。How to 'select' from XML with namespaces? should help you with what you need.
XML Namespaces are simply a way of enabling uniqueness of XML elements and attributes. If two users/companies/whatevers define a
<DVD>
element the namespace is the way in which they are kept separate. So you could, for example, process two different<DVD>
elements by selecting each one along with the appropriate namespace in which it was defined.