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

Subject: Re: How to use for-each to get comma searated list?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 02 Dec 1998 14:43:44 -0500
At 98/12/02 12:43 -0500, Farrukh S. Najmi 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. 

I can almost get you there, but I don't understand why
IE5.0beta2(5.00.0910.1309) isn't completing the job for me.  I'll still
work on it, but I thought I'd post these intermediate results in case
someone sees what I'm doing wrong.  The script below will display the
correct number of commas, but not the values!

My reference for using the "end()" function is from Microsoft's own
documentation ... I hadn't seen the function before and I only looked when
you posted your question.

Below this example I have an example conforming to
http://www.w3.org/TR/1998/WD-xsl-19980818 that seems to work just fine.

===============================8<==============================
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl";>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/Doc">     <!-- first prepare commentary around list -->
  <BODY>
       valueList="
       <xsl:apply-templates select="Section"/>
       "
  </BODY>
</xsl:template> 

                 <!-- when more than one in list, punctuation after others -->
<xsl:template match="Section" >
   <xsl:apply-templates/>
   ,
</xsl:template> 

              <!-- no punctuation after last (same as if only one in list) -->
<xsl:template match="Section[end()]" >
   <xsl:apply-templates/>
</xsl:template> 

<xsl:template match="Foo">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="Value">
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>
===============================8<==============================



Restricting oneself to http://www.w3.org/TR/1998/WD-xsl-19980818, the
following works just fine:

===============================8<==============================
D:\temp\xslcomma>type test.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl";
                xmlns="http://www.w3.org/TR/REC-html40";
                result-ns="">

<xsl:template match="/Doc">     <!-- first prepare commentary around list -->
  <BODY>
       <xsl:text>valueList="</xsl:text>
       <xsl:for-each select="Section/Foo">
          <xsl:process select="Value"/>
       </xsl:for-each> 
       <xsl:text>"</xsl:text>
  </BODY>
</xsl:template> 

              <!-- no punctuation after last (same as if only one in list) -->
<xsl:template match="Section[last-of-type()]/Foo/Value" >
   <xsl:process-children/>
</xsl:template> 

                 <!-- when more than one in list, punctuation after others -->
<xsl:template match="Section/Foo/Value" >
   <xsl:process-children/>
   <xsl:text>,</xsl:text>
</xsl:template> 

</xsl:stylesheet>
D:\temp\xslcomma>type test1.xml
<?xml version="1.0"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<Doc>
  <Section>
    <Foo><Value>3.75</Value></Foo>
  </Section>
</Doc>

D:\temp\xslcomma>call xsl test1.xml test.xsl test1.htm
D:\temp\xslcomma>type test1.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<BODY>valueList=&quot;3.75&quot;</BODY>


D:\temp\xslcomma>type test2.xml
<?xml version="1.0"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<Doc>
  <Section>
    <Foo><Value>3.75</Value></Foo>
  </Section>

  <Section>
    <Foo><Value>2.25</Value></Foo>
  </Section>
</Doc>

D:\temp\xslcomma>call xsl test2.xml test.xsl test2.htm
D:\temp\xslcomma>type test2.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<BODY>valueList=&quot;3.75,2.25&quot;</BODY>

D:\temp\xslcomma>type test3.xml
<?xml version="1.0"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<Doc>
  <Section>
    <Foo><Value>3.75</Value></Foo>
  </Section>

  <Section>
    <Foo><Value>2.25</Value></Foo>
  </Section>

  <Section>
    <Foo><Value>1.61</Value></Foo>
  </Section>
</Doc>

D:\temp\xslcomma>call xsl test3.xml test.xsl test3.htm
D:\temp\xslcomma>type test3.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<BODY>valueList=&quot;3.75,2.25,1.61&quot;</BODY>

D:\temp\xslcomma>
===============================8<==============================


--
G. Ken Holman         mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.  http://www.CraneSoftwrights.com/s/
Box 266,                                V: +1(613)489-0999
Kars, Ontario CANADA K0A-2E0            F: +1(613)489-0995
Training:   http://www.CraneSoftwrights.com/s/schedule.htm
Resources: http://www.CraneSoftwrights.com/s/resources.htm
Shareware: http://www.CraneSoftwrights.com/s/shareware.htm


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


Current Thread