Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xslt "apply-templates" question

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
Hi,

Hmmm, I can't quite figure out how to do this with xslt. I'm extremely new to this technology, however, so I'm sure(hope?) there's an easy solution...

Let's say a portion of my to-be-transformed xml file looks as so:
Code:
<data>d1</data>
<data>d2</data>
.
.
.
<data>dn</data>
and I need to format this as text as follows:
Code:
d1,
d2,
.
.
.
dn
HOW can I code something to append a comma after the first (n-1) elements, but not the nth??? Otherwise, the resultant (output)code won't parse properly.

I've tried something like this:
Code:
<xsl:template match=&quot;some context...&quot;>
    <xsl:apply-templates select=&quot;data&quot;>
</xsl:template>

<xsl:template match=&quot;data&quot;>
    <xsl:value-of select=&quot;.&quot;/><xsl:text>,</xsl:text>
</xsl:template>
but of course this appends a comma after EVERY element, including the nth... How can I &quot;know&quot; when I'm at the last node of the &quot;data&quot;-node set, so as to avoid appending a comma? I could also prepend commas, but in that case I'd need to be able to know when I'm processing the first &quot;data&quot; node, so as to avoid prepending a comma to it... That might be easier, maybe? To isolate the first-node-processed case? Or is something like this patently impossible (I sure hope not!!)

Any help *greatly* appreciated.. I need to figure this out in order to generate the code I need at work!!!

Thanks!!
dora



 
<xsl:if test=&quot;following-sibling::data&quot;><xsl:text>,</xsl:text></xsl:if>

-pete
 
Thanks, that worked perfectly!!!

:)
dora
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top