如何使用“insref”和“pref”对下面的元素进行分组我曾尝试在 xslt 版本 1 中使用生成密钥 ID。
XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
<mylist>
<cd id="1" insref="#10">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd id ="2" insref="#20" pref="#1">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd id="3" insref="#520" pref="#1">
<title>Lonly heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd id="4" insref="#56" pref="#1">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd id="5" insref="#56" pref="#2">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd id="6" insref="#56" pref="#2">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd id="7" insref="#56" pref="#2">
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd id="8" insref="#78" pref="#2">
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd id="9" insref="#45" pref="#1">
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd id="10" insref="#45" pref="#2">
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
</mylist>
</catalog>
期望的结果:
TITLE ARTIST ID INSREF PREF Qty
Empire Burlesque Bob Dylan 1 10
Hide your heart Bonnie Tyler 2 20 1 1
Lonly heart Bonnie Tyler 3 520 1 1
Still got the blues Gary Moore 4 56 1 1
Still got the blues Gary Moore 4 56 2 3
Maggie May Rod Stewart 8 78 2 1
Romanza Andrea Bocelli 9 45 1 1
Romanza Andrea Bocelli 10 45 2 1
这就是我所拥有的。我也在使用密钥生成功能。知道这个 xslt 有什么问题。至于计数,我正在考虑获得唯一键计数。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="by-accessRefs" match="cd" use="concat(@insref,@pref)"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
<th>INSREF</th>
<th>PREF</th>
</tr>
<xsl:for-each select="catalog/mylist/cd[generate-id(.) = generate-id(key('by-accessRefs',concat(@insref,@pref)[1])]">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="@insref"/></td>
<td><xsl:value-of select="@pref"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>