如何使用'insref'和'pref'对下面的元素进行分组 我尝试在xslt版本1中使用generate key id。
<?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>
答案 0 :(得分:1)
此XSLT 1.0转换:
<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:key name="kCDByProps" match="cd" use=
"concat(title,'+',artist,'+',@insref,'+',@pref)"/>
<xsl:template match="/*">
<table>
<thead>
<tr>
<td>TITLE</td><td>ARTIST</td><td>ID</td><td>INSREF</td><td>PREF</td><td>Qty</td>
</tr>
</thead>
<xsl:apply-templates select=
"mylist/cd
[generate-id()
=
generate-id(key('kCDByProps',
concat(title,'+',artist,'+',@insref,'+',@pref)
)[1])
]
"/>
</table>
</xsl:template>
<xsl:template match="cd">
<xsl:variable name="vGroup" select=
"key('kCDByProps', concat(title,'+',artist,'+',@insref,'+',@pref))"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="substring(@insref, 2)"/></td>
<td><xsl:value-of select="substring(@pref, 2)"/></td>
<xsl:variable name="vCount" select=
"count($vGroup[@pref = current()/@pref])"/>
<td>
<xsl:if test="$vCount"><xsl:value-of select="$vCount"/></xsl:if>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时:
<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>
生成以下HTML输出:
<table>
<thead>
<tr>
<td>TITLE</td>
<td>ARTIST</td>
<td>ID</td>
<td>INSREF</td>
<td>PREF</td>
<td>Qty</td>
</tr>
</thead>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
<td>1</td>
<td>10</td>
<td/>
<td/>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
<td>2</td>
<td>20</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Lonly heart</td>
<td>Bonnie Tyler</td>
<td>3</td>
<td>520</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Still got the blues</td>
<td>Gary Moore</td>
<td>4</td>
<td>56</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Still got the blues</td>
<td>Gary Moore</td>
<td>5</td>
<td>56</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>Maggie May</td>
<td>Rod Stewart</td>
<td>8</td>
<td>78</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>Romanza</td>
<td>Andrea Bocelli</td>
<td>9</td>
<td>45</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Romanza</td>
<td>Andrea Bocelli</td>
<td>10</td>
<td>45</td>
<td>2</td>
<td>1</td>
</tr>
</table>
在浏览器中查看时似乎是想要的正确结果:
答案 1 :(得分:0)
我认为您需要的输出是HTML表格:
样式表文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xs xd"
version="2.0">
<xsl:output method="html" indent="yes"/>
<xsl:key name="kTitle" match="title" use="text()"/>
<xsl:template match="/">
<table>
<thead>
<tr>
<th>TITLE</th>
<th>ARTIST</th>
<th>ID</th>
<th>INSREF</th>
<th>PREF</th>
<th>Qty</th>
</tr>
</thead>
<tbody>
<xsl:for-each-group select="//cd" group-by="@id">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="substring-after(@insref,'#')"/></td>
<td><xsl:value-of select="substring-after(@pref,'#')"/></td>
<td><xsl:value-of select="count(key('kTitle',title/text()))"/></td>
</tr>
</xsl:for-each-group>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
结果文档:
<table>
<thead>
<tr>
<th>TITLE</th>
<th>ARTIST</th>
<th>ID</th>
<th>INSREF</th>
<th>PREF</th>
<th>Qty</th>
</tr>
</thead>
<tbody>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
<td>1</td>
<td>10</td>
<td></td>
<td>1</td>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
<td>2</td>
<td>20</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Lonly heart</td>
<td>Bonnie Tyler</td>
<td>3</td>
<td>520</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Still got the blues</td>
<td>Gary Moore</td>
<td>4</td>
<td>56</td>
<td>1</td>
<td>4</td>
</tr>
<tr>
<td>Still got the blues</td>
<td>Gary Moore</td>
<td>5</td>
<td>56</td>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>Still got the blues</td>
<td>Gary Moore</td>
<td>6</td>
<td>56</td>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>Still got the blues</td>
<td>Gary Moore</td>
<td>7</td>
<td>56</td>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>Maggie May</td>
<td>Rod Stewart</td>
<td>8</td>
<td>78</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>Romanza</td>
<td>Andrea Bocelli</td>
<td>9</td>
<td>45</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>Romanza</td>
<td>Andrea Bocelli</td>
<td>10</td>
<td>45</td>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>