Re: [exslt] About set:distinct template
by Jeni Tennison other posts by this author
Nov 22 2001 12:27PM messages near this date
Re: [exslt] About set:distinct template
|
Re: [exslt] About set:distinct template
Hi,
> There is the xml source code as:
> <doc>
> <city name="Paris"
> country="France" />
> <city name="Madrid"
> country="Spain" />
> <city name="Vienna"
> country="Austria" />
> <city name="Barcelona"
> country="Spain" />
> <city name="Salzburg"
> country="Austria" />
> <city name="Bonn"
> country="Germany" />
> <city name="Lyon"
> country="France" />
> <city name="Hannover"
> country="Germany" />
> <city name="Calais"
> country="France" />
> <city name="Berlin"
> country="Germany" />
> </doc>
>
> I hope to get the result as:
>
> France:
> Paris, Lyon, Calais
> Spain:
> Madrid, Barcelona
> Austria:
> Vienna, Salzburg
> Germany:
> Bonn, Hannover, Berlin
>
> I do not know how to use the result of set:distinct template to do so.
Create a key that indexes each city based on its country:
<xsl:key name="cities" match="city" use="@country" />
That allows you to quickly get at all the cities from a particular
country using:
key('cities', $country)
Now use the set:distinct template to get the unique @country
attributes in the document:
<xsl:call-template name="set:distinct">
<xsl:with-param name="nodes" select="/doc/city/@country" />
</xsl:call-template>
Then create a template that matches the country attributes in
set:distinct mode. In there, give the value of that country attribute,
a colon, a new line, and then iterate over the cities that you
retrieve using the key, as follows:
<xsl:template match="@country" mode="set:distinct">
<xsl:text> 
</xsl:text>
<xsl:value-of select="." />
<xsl:text> :
 </xsl:text>
<xsl:for-each select="key('cities', .)">
<xsl:value-of select="@name" />
<xsl:if test="position() != last()"> , </xsl:if>
</xsl:for-each>
</xsl:template>
The countries come out in a slightly different order from the one
you're expecting, but hopefully that isn't a problem.
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
_______________________________________________
exslt mailing list
list@[...].org
http://www.exslt.org/list
Thread:
=?gb2312?B?sNfRqbfJ?=
Bai Xuefei
Bai Xuefei
Jeni Tennison
Jeni Tennison
|