注1:這是我的第一個問題。請溫柔。
注2:是的,這是作業。我討厭尋求幫助,但我碰到了一堵磚牆。如果你不想告訴我答案,建議在哪裏尋找答案將同樣表示讚賞。謝謝。<xsl:apply-templates>中的XSL:Xpath查詢不返回任何結果
我想寫一個XSLT表,查看目標XML文件中每個DVD元素的流派屬性。它應該返回一個列表,其中每個列出的每個不同的值都只有一次,並且每個值的計數次數都被找到。
我認爲我的XPath是錯誤的。爲了調試,我試過使用<xsl:value-of select="foo">
語句來嘗試不同的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>
提示:您的XML綁定到名稱空間:'xmlns =「http://example.com/dvdlist」'。如果要在XSLT/XPath中尋址這些元素,則需要聲明名稱空間並在XPath中使用前綴,或者在謂詞過濾器中匹配'namespace-uri()'。 – 2011-04-17 19:35:05
哇,XSLT的作業。我不記得我的家庭作業從未如此有趣! +1寫得很好的問題。 – andyb 2011-04-17 20:08:04
+1希望每一個問題都能得到這個精心思考! – rekaszeru 2011-04-17 20:22:56