Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate data in XSL?

Tags:

xslt

I have an XML file with the following structure:

<root>
    <level1>
        <level2>
            <value>A</value>
            <value>B</value>
            <value>C</value>
            <value>D</value>
            <value>E</value>
            <value>F</value>
        </level2>
    </level1>
</root>

Guess I want to concatenate only the first three values in XSL in order to get ABC. How do I do it?

like image 663
RegedUser00x Avatar asked Dec 11 '22 22:12

RegedUser00x


2 Answers

Assuming the focus item is the level2 node, you can use a XSLT 1.0 sequence constructor like...

<xsl:value-of select="concat(value[1],value[2],value[3])" />

...or in XSLT 2.0...

<xsl:value-of select="for $i in 1 to 3 return value[$i]" />
like image 82
Sean B. Durkin Avatar answered Jan 27 '23 18:01

Sean B. Durkin


You can do this with one piece of XPath:

/root/level1/level2/value[position() &lt;= 3]/text()

To put that into an XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:copy-of select="root/level1/level2/value[position() &lt;= 3]/text()" />
  </xsl:template>
</xsl:stylesheet>
like image 39
Flynn1179 Avatar answered Jan 27 '23 20:01

Flynn1179