This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Many sets of eyes ...
- From: "Thomas B. Passin" <tpassin at mitretek dot org>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Mon, 11 Feb 2002 18:39:02 -0500
- Subject: Re: [xsl] Many sets of eyes ...
- References: <E16aIiK-00047X-00@oceanus.uk.clara.net>
- Reply-to: xsl-list at lists dot mulberrytech dot com
[Curtis Burisch]
> ...
> Given the xml source:
>
> <?xml version="1.0" encoding="iso-8859-1"?>
> <root>
> <wrap>
> <joe>Apples</joe>
> </wrap>
> <wrap>
> <joe>Bananas</joe>
> </wrap>
> <wrap>
> <ann>Pears</ann>
> </wrap>
> <wrap>
> <joe>Oranges</joe>
> </wrap>
> </root>
>
> And the desired output:
>
> Joe says: "Apples, Bananas."
> Ann says: "Pears."
> Joe says: "Oranges."
>
I did a slightly different take on this and assumed that you would want to
collect all of joe's preferences together, like this:
Ann says: "Pears."
Joe says: "Apples, Bananas,Oranges."
Even if this is not what you really want, it's interesting to see how it
works out. I have not completely handled putting in commas everywhere
except a period for the last item - I leave this to the reader. I also
haven't translated the first character of the name to upper case - Jenni
covered that. I also sorted the result by name. The solution is very
compact without those refinements:
==========================================================
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text'/>
<!-- key for using the Muenchian method of getting unique node sets-->
<xsl:key name='wrappers' match='wrap' use='name(*)'/>
<!-- Elements with unique person names -->
<xsl:variable name='unique'
select='/root/wrap[generate-id(key("wrappers",name(*))[1])=generate-id(.)]'/
>
<xsl:template match="/">
<xsl:for-each select='$unique'>
<xsl:sort select='name(*)'/>
<xsl:variable name='theName' select='name(*)'/>
<xsl:value-of select='$theName'
/> says: <xsl:for-each select='key("wrappers",$theName)'
><xsl:value-of select='normalize-space(.)'
/>, </xsl:for-each><xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
========================================================
The slightly odd formatting is an easy way to control the output format
while still having short line lengths in the stylesheet (better for
emailing), and the character reference is necessary on my (Windows)
system to get the line feed to display. Here is the result (I added another
person, bob, to the data, just for fun):
===========================
ann says: Pears,
bob says: Peaches,
joe says: Apples, Bananas, Oranges,
===========================
This was interesting because the usual examples for getting unique node-sets
assume you know what the target elements are named, but not in this case.
Cheers,
Tom P
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list