我想在我的XSLT中使用Muenchian分組來分組匹配的節點,但我只想在父節點內分組,而不是跨整個源XML文檔。Muenchian分組 - 在一個節點內的組,而不是在整個文檔中
鑑於XSLT和XML如下(道歉的我的樣本代碼的長度):
XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:key name="contacts-by-surname" match="contact" use="surname" />
<xsl:template match="records">
<xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[1]) = 1]">
<xsl:sort select="surname" />
<xsl:value-of select="surname" />,<br />
<xsl:for-each select="key('contacts-by-surname', surname)">
<xsl:sort select="forename" />
<xsl:value-of select="forename" /> (<xsl:value-of select="title" />)<br />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML
<root>
<records>
<contact id="0001">
<title>Mr</title>
<forename>John</forename>
<surname>Smith</surname>
</contact>
<contact id="0002">
<title>Dr</title>
<forename>Amy</forename>
<surname>Jones</surname>
</contact>
<contact id="0003">
<title>Mrs</title>
<forename>Mary</forename>
<surname>Smith</surname>
</contact>
<contact id="0004">
<title>Ms</title>
<forename>Anne</forename>
<surname>Jones</surname>
</contact>
<contact id="0005">
<title>Mr</title>
<forename>Peter</forename>
<surname>Smith</surname>
</contact>
<contact id="0006">
<title>Dr</title>
<forename>Indy</forename>
<surname>Jones</surname>
</contact>
</records>
<records>
<contact id="0001">
<title>Mr</title>
<forename>James</forename>
<surname>Smith</surname>
</contact>
<contact id="0002">
<title>Dr</title>
<forename>Mandy</forename>
<surname>Jones</surname>
</contact>
<contact id="0003">
<title>Mrs</title>
<forename>Elizabeth</forename>
<surname>Smith</surname>
</contact>
<contact id="0004">
<title>Ms</title>
<forename>Sally</forename>
<surname>Jones</surname>
</contact>
<contact id="0005">
<title>Mr</title>
<forename>George</forename>
<surname>Smith</surname>
</contact>
<contact id="0006">
<title>Dr</title>
<forename>Harry</forename>
<surname>Jones</surname>
</contact>
</records>
</root>
RESULT
Jones,
Amy (Dr)
Anne (Ms)
Harry (Dr)
Indy (Dr)
Mandy (Dr)
Sally (Ms)
Smith,
Elizabeth (Mrs)
George (Mr)
James (Mr)
John (Mr)
Mary (Mrs)
Peter (Mr)
我怎麼組的每個<records>
內實現這一結果:
Jones,
Amy (Dr)
Anne (Ms)
Indy (Dr)
Smith,
John (Mr)
Mary (Mrs)
Peter (Mr)
Jones,
Harry (Dr)
Mandy (Dr)
Sally (Ms)
Smith,
Elizabeth (Mrs)
George (Mr)
James (Mr)
+1好問題 – 2009-11-18 05:18:35
克里斯蒂安,在你想要的結果中,姓氏沒有在姓氏內排序。我假設他們應該是因爲你明確地在你的xslt的forename排序。 – 2009-11-18 05:41:19
有關訂購的好處 - 已更新問題以在結果中包含已排序的名稱。 – kristian 2009-11-18 05:59:50