The position() and last() take the list of all CUSTOMER elements as reference –
not just those with “Amount = 0”.
To fix your solution just put the condition in the select:
<xsl:for-each select="CUSTOMERS/CUSTOMER[Amount = 0]">
<xsl:value-of select="ID"/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
With XPath 2.0 you could simplify this to a single xpath expression using
string-join:
<xsl:value-of select="string-join(CUSTOMERS/CUSTOMER[Amount = 0]/ID, ',')"/>
Patrik
------------------------------------------------------------------
Systemarchitektur & IT-Projekte
Tel: +49 40 33449-1142
Fax: +49 40 33449-1400
E-Mail:
Patrik(_dot_)Stellmann(_at_)gdv-dl(_dot_)de<mailto:Patrik(_dot_)Stellmann(_at_)gdv-dl(_dot_)de>
[https://www.gdv-dl.de/fileadmin/user_upload/bild/Bilder_auf_Website/Zentralruf/160921_Entwurf_DKM-Banner_Signatur_65_small.jpg]<http://www.die-leitmesse.de/zentralruf-autoversicherer/2016>
Von: Rahul Singh rahulsinghindia15(_at_)gmail(_dot_)com
[mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com]
Gesendet: Freitag, 7. Oktober 2016 09:23
An: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Betreff: [xsl] How to XSLT concat string, remove last comma
Hi,
I need to build up a string using XSLT and separate each string with a comma
but not include a comma after the last string. Here i have mentioned our XSL
but i am geetting comma in my output.
Input:
<?xml version="1.0"?>
<CUSTOMERS>
<CUSTOMER>
<ID>441</ID>
<Item_no>24</Item_no>
<Amount>0</Amount>
</CUSTOMER>
<CUSTOMER>
<ID>900817</ID>
<Item_no>28</Item_no>
<Amount>0</Amount>
</CUSTOMER>
<CUSTOMER>
<ID>00081</ID>
<Item_no>4</Item_no>
<Amount>1</Amount>
</CUSTOMER>
</CUSTOMERS>
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<CUSTOMERS>
<CUSTOMER>
<Id>
<xsl:for-each select="CUSTOMERS/CUSTOMER">
<xsl:if test="Amount = 0">
<xsl:value-of select="ID"/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
</Id>
</CUSTOMER>
</CUSTOMERS>
</xsl:template>
</xsl:stylesheet>
My Output:
<?xml version="1.0" encoding="UTF-8"?>
<CUSTOMERS>
<CUSTOMER>
<Id>441,900817,</Id>
</CUSTOMER>
</CUSTOMERS>
Expected output:
<?xml version="1.0" encoding="UTF-8"?>
<CUSTOMERS>
<CUSTOMER>
<Id>441,900817</Id>
</CUSTOMER>
</CUSTOMERS>
XSL-List info and archive<http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe<-list/2718916> (by email<>)
GDV Dienstleistungs-GmbH & Co. KG
Glockengießerwall 1
D-20095 Hamburg
www.gdv-dl.de
Sitz und Registergericht: Hamburg
HRA 93 894
USt.-IdNr : DE 205183123
Komplementärin:
GDV Beteiligungsgesellschaft mbH
Sitz und Registergericht: Hamburg
HRB 71 153
Geschäftsführer:
Dr. Jens Bartenwerfer
Michael Bathke
------------------------------------------------------------------
Diese E-Mail und alle Anhänge enthalten vertrauliche und/oder rechtlich
geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese
E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und
vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe der E-Mail ist nicht gestattet.
This e-mail and any attached files may contain confidential and/or privileged
information. If you are not the intended recipient (or have received this
e-mail in error) please notify the sender immediately and destroy this e-mail.
Any unauthorised copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--