Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2007, 12:01 PM
Registered User
 
Join Date: Nov 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generating a comma-delimited data in XSLT

I have some data in xml that looks like this:

<AllServices>
  <Service ID="1">a</Service>
  <Service ID="2">b</Service>
  <Service ID="3" />
  <Service ID="4">d</Service>
</AllServices>

The Service element values are dynamic and each element doesn't contain its value all the time. I'm trying to display the data as comma delimited via XSLT, so this is what I have:

<xsl:for-each select="AllServices/Service">
  <xsl:if test=".!=''">
    <xsl:value-of select="." />
    <xsl:if test="position()!=last()">, </xsl:if>
  </xsl:if>
</xsl:for-each>

This only works when the last Service element contains a value. If the last Service element does not contain a value, it leaves the comma at the end of the list like this: a, b,

I need to get rid of the last comma even when the last element value is not present. Can someone tell me how I can achieve this? Thanks in advance!

Sam
 
Old November 30th, 2007, 12:06 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Change the <xsl:for-each> to select="AllServices/Service[.!='']"

and then get rid of the first <xsl:if> test.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 30th, 2007, 12:12 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Change

<xsl:for-each select="AllServices/Service">
  <xsl:if test=".!=''">
    <xsl:value-of select="." />
    <xsl:if test="position()!=last()">, </xsl:if>
  </xsl:if>
</xsl:for-each>

to

<xsl:for-each select="AllServices/Service[.!='']">
    <xsl:value-of select="." />
    <xsl:if test="position()!=last()">, </xsl:if>
</xsl:for-each>

Or in XSLT 2.0 it becomes a one-liner:

<xsl:value-of select="AllServices/Service[.!='']" separator=", "/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Comma delimited file to Excel Sheet vinod_yadav1919 VB How-To 0 August 5th, 2005 10:09 AM
Comma delimited text files bmurrin Beginning VB 6 8 February 26th, 2004 02:07 PM
Comma Delimited Update? mariakovacs Access ASP 6 October 13th, 2003 03:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.