[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Many sets of eyes ...

Subject: Re: Many sets of eyes ...
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Mon, 11 Feb 2002 18:39:02 -0500
muenchian eyes.
[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"
<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'


<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>&#10;&#13;</xsl:text>

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 &#13; 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.


Tom P

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.