Venkata Penumatsa
Venkata Penumatsa

Reputation: 411

How to separate similar data elements with a symbol(;) in XSLT

My source code is below. I am having similar data elements with different values. Below example I am having 3 phone numbers and I wants to have all in single line with a separator (;) I used concatenate function but no use

<?xml version="1.0" encoding="UTF-8"?>
<report_entry>
<report_data>
    <Phone_Numbers_-_Home Descriptor="+44 (233) 45666">
        <ID type="WID">16001e91f6ba1ewa583d2eafc901baa6</ID>
    </Phone_Numbers_-_Home>
    <Phone_Numbers_-_Home Descriptor="+44 (234) 56777">
        <ID type="WID">16001ewsf6ba108a584e5d776077bbe3</ID>
    </Phone_Numbers_-_Home>
    <Phone_Numbers_-_Home Descriptor="+44 (123) 23450987">
        <ID type="WID">16001e91f6ba1b8a582ab3e49446b92c</ID>
    </Phone_Numbers_-_Home>
</report_data>

Expected outcome is below. I need ";" in b/w data values

+44 (233) 45666 +44;(234) 56777;+44 (123) 23450987

I am using below code, where symbol (;) is coming at the end of string which is not needed. I need in b/w the phone values and doesn't need at the end of last value

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"  
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xs xd"
version="2.0">
<xsl:output method="text" omit-xml-declaration="yes"></xsl:output>

<xsl:template match="/report_entry/report_data">      

    <xsl:value-of select="Phone_Numbers_-_Home/@Descriptor"/> <xsl:value-of select="';'"/>


</xsl:template>

Upvotes: 2

Views: 221

Answers (3)

Amrendra Kumar
Amrendra Kumar

Reputation: 1816

To achieve your desire output we have multiple way to do like concat(), string-join() and as suggested @Martin use @separator:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd"
    version="2.0">

    <xsl:output method="text" omit-xml-declaration="yes"/>

    <xsl:template match="/report_entry/report_data">
      <!--  <xsl:value-of select="Phone_Numbers_-_Home/@Descriptor" separator="; "/>-->
        <xsl:value-of select="string-join(Phone_Numbers_-_Home/@Descriptor, '; ')"/>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 0

imran
imran

Reputation: 461

<xsl:template match="//Phone_Numbers_-_Home">
        <xsl:value-of select="@Descriptor"/>
        <xsl:if test="not(position()= last())">
            <xsl:text>;</xsl:text>
        </xsl:if>
   </xsl:template>

Check this answer

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167716

It should be easy to find a way to separate items selected by xsl:value-of select="Phone_Numbers_-_Home/@Descriptor" by simply looking at the spec https://www.w3.org/TR/xslt-30/#element-value-of as there you find an attribute named separator so <xsl:value-of select="Phone_Numbers_-_Home/@Descriptor" separator=";"/> is all you need.

Upvotes: 2

Related Questions