[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: How to use for-each to get comma searated list?


Subject: Re: How to use for-each to get comma searated list?
From: Tom Hughes <tom-xsl-list@xxxxxxxxxxxxxxxxxxx>
Date: Wed, 02 Dec 1998 19:05:11 +0000

In message <36657C42.F44B0E6A@xxxxxxxxx>
          "Farrukh S. Najmi" <najmi@xxxxxxxxx> wrote:


> I am trying to use the current version of XSL (ala IE5.0 beta2) to
> format the values inside a XML element into a comma separated list. The
> problem is that I cannot figure out how to not have a comma after the
> last element in the list. Any help would be appreciated. Following is
> simplified XML file, desired output and my attempt at the XSL file:

There's an example in the current draft of doing exactly this. Basically
you replace this:

  <xsl:text>,</xsl:text>

with this:

  <xsl:if test=".[not-last-of-type()]">,</xsl:if>

That doesn't work with XT though and, looking at the draft I couldn't
see anything in the grammar that would allow it.

I'm also not sure it's sufficient in your case because you need to
look at the Section element not the Value element. This might do it
though:

  <xsl:if test="ancestor(Section)[not-last-of-type()]">,</xsl:if>

Except that you can't qualify an anchor so that isn't going to work
either... I suspect you'll need to loop on the Section elements and
then process Foo/Value like this:

  <xsl:for-each select="Section">
    <xsl:process select="Foo/Value"/>
    <xsl:if test=".[not-last-of-type()]">,</xsl:if>
  </xsl:for-each>

But that won't work if there is more than one Foo in a Section and
suffers from the same dubiousness as the example in the draft.

It's very horrible really. I've been fighting similar problems and
sometimes I can find a way round them and sometimes I can't.

Tom

-- 
Tom Hughes (tom@xxxxxxxxxxxxxxxxxxx)
http://www.compton.demon.co.uk/
...Sign on bank: "FREE BOTTLE OF CHIVAS WITH EVERY MILLION-DOLLAR DEPOSIT."


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



Current Thread
Keywords