1

I've been trying to concatenate two XML elements into one.

Input File

<Location>
        <Location_Name>Test Location</Location_Name>
        <Address>
            <Country>USA</Country>
            <Address_Line_Data>2 Sample St</Address_Line_Data>
            <Address_Line_Data>35 Wentworth Ave</Address_Line_Data>
            <Municipality>Meier</Municipality>
        </Address>
</Location>

Desired Output file

<Location>
        <Location_Name>Test Location</Location_Name>
        <Address>
            <Country>USA</Country>
            <Address_Line_Data>2 Sample St - 35 Wentworth Ave</locc:Address_Line_Data>
            <Municipality>Meier</Municipality>
        </Address>
</Location>

How can I achieve that using XSLT ?

I've tried the following but no success.

<?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"
    exclude-result-prefixes="xs" version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <!-- Identity template -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

<xsl:template match="Location/Address/Address_line_Data"">
    <xsl:for-each select=".">
        <xsl:value-of select="concat(.,' - ')"/>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
1
  • 1
    <Address_Line_Data>2 Sample St</locc:Address_Line_Data> is not even well-formed, neither in the input nor in the output. Commented Oct 21, 2020 at 12:00

2 Answers 2

1

Use

  <xsl:template match="Address_Line_Data[1]">
      <xsl:copy>
          <xsl:value-of select="../Address_Line_Data" separator=" - "/>
      </xsl:copy>
  </xsl:template>
  
  <xsl:template match="Address_Line_Data[position() gt 1]"/>

plus the identity transformation template you have.

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer, +1 from my side!
Thanks a lot @Martin Honnen. That's exactly what I was looking for. Just for cosmetic sake, I've also added the tag ` <xsl:strip-space elements="*"/>` which removes the blank line left by the concatenation. Cheers
0

Here's a way you could do this. Make sure your input XML is well-formed, there's a "locc:" that shouldn't be there. Also when you refer to an element or attribute, you need to use the exact name, as it is in the input.

<?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"
    exclude-result-prefixes="xs" version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <!-- Identity template -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Address_Line_Data[position()=1]">
        <xsl:copy>
            <xsl:value-of select="../Address_Line_Data" separator=" - "/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Address_Line_Data[position()!=1]"/>

</xsl:stylesheet>

See it working here: https://xsltfiddle.liberty-development.net/93nwMnV

1 Comment

Thanks a lot @Sebastien. That's exactly what I was looking for. Just for cosmetic sake, I've also added the tag ` <xsl:strip-space elements="*"/>` which removes the blank line left by the concatenation. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.