提供的代码中存在许多错误,其中最明显的是:
<xsl:for-each select=
"groups/elements/category/types[@name='POP']/language
| groups/elements[delivery]/category/types[@name='POP']/language">
types
元素有一个属性Name
——不是一个name
属性。XPath 区分大小写,不同大小写的名称被视为不同。
第二个逻辑错误:
上面的联合等价于它的第一个参数:
groups/elements/category/types[@name='POP']/language
因为第二个参数(nodeset)包含在第一个参数(node-set)中。
也许您想在第二个参数中指定一些不同的东西?
第三个明显的错误是一个language
元素没有命名的子元素PostDate
或者name
——因此里面的代码xsl:for-each
不会产生任何东西。
第四个错误是 that category
is not a child of elements
-- 因此select
属性 ofxsl:for-each
甚至不选择单个节点。
解决方案:
纠正上述所有错误后,我的猜测是你想要这样的东西:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<body>
<h2>display 9 Items</h2>
<xsl:for-each select="groups/elements[.//category/types[@Name='POP']/language]">
<xsl:sort select="PostDate" order="descending"/>
<xsl:if test="not(position() > 9)">
<b><xsl:value-of select="PostDate" /></b> -
<font color='red'><xsl:value-of select="name" /></font>
<p></p>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<groups>
<elements>
<name>//1</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="POP" />
<types Code="POP/MUSIC" Name="POP" >
<language>Italian</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2012-03-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//1+1</name>
<BOD></BOD>
<description/>
<delivery/>yes
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="POP" />
<types Code="POP/MUSIC" Name="POP" >
<language>Italian</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2012-03-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//2</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="PureMusic" />
<types Code="POP/MUSIC" Name="JAZZ" >
<language>No language</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2011-03-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//3</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="JAZZ/DANCE" Name="JAZZ" />
<types Code="JAZZ/MUSIC" Name="BLUES">
<language>English</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2012-05-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//4</name>
<BOD></BOD>
<description/>
<delivery/>yes
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="Blues/DANCE" Name="POP" />
<types Code="Blues/MUSIC" Name="POP" >
<language>No language</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2012-01-14</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//5</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="DANCE/DANCE" Name="POP" />
<types Code="RAP/MUSIC" Name="POP" >
<language>No language</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2010-03-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//6</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/HALL" Name="HALL" />
<types Code="POP/MUSIC" Name="POP" >
<language>German</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2009-10-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//7</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="JAZZ/DANCE" Name="POP" />
<types Code="BLUES/MUSIC" Name="POP" >
<language>Korean</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2001-11-05</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//8</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="POP" />
<types Code="POP/MUSIC" Name="POP" >
<language>English</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2004-03-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//9</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="HIPHIP" />
<types Code="POP/MUSIC" Name="POP" >
<language>Chinese</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2007-02-18</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//10</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="POP" />
<types Code="POP/MUSIC" Name="POP" >
<language>Janpanese</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2002-03-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//11</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/JAZZ" Name="POP" />
<types Code="POP/MUSIC" Name="JAZZ" >
<language>Spanish</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2012-03-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//12</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="POP" />
<types Code="POP/MUSIC" Name="POP">
<language>French</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2006-01-04</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//13</name>
<BOD></BOD>
<description/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="POP" />
<types Code="POP/MUSIC" Name="POP" >
<language>English</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2012-05-02</PostDate>
<trackno></trackno>
</elements>
<elements>
<name>//14</name>
<BOD></BOD>
<description/>
<delivery/>
<URL></URL>
<Hobbbies>
<Sport></Sport>
<Music>
<category>
<types Code="POP/DANCE" Name="BB" />
<types Code="POP/MUSIC" Name="DANCE" >
<language>Spanish</language>
</types>
</category>
</Music>
</Hobbbies>
<Size></Size>
<PostDate>2002-06-10</PostDate>
<trackno></trackno>
</elements>
</groups>
结果是:
<html>
<body>
<h2>display 9 Items</h2><b>2012-05-02</b> -
<font color="red">//13</font><p></p><b>2012-03-04</b> -
<font color="red">//1</font><p></p><b>2012-03-04</b> -
<font color="red">//1+1</font><p></p><b>2012-01-14</b> -
<font color="red">//4</font><p></p><b>2010-03-04</b> -
<font color="red">//5</font><p></p><b>2009-10-04</b> -
<font color="red">//6</font><p></p><b>2007-02-18</b> -
<font color="red">//9</font><p></p><b>2006-01-04</b> -
<font color="red">//12</font><p></p><b>2004-03-04</b> -
<font color="red">//8</font><p></p>
</body>
</html>
它在浏览器中显示为:
显示 9 项
2012-05-02 - //13
2012-03-04 - //1
2012-03-04 - //1+1
2012-01-14 - //4
2010-03-04 - //5
2009-10-04 - //6
2007-02-18 - //9
2006-01-04 - //12
2004-03-04 - //8