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?
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]" />
You can do this with one piece of XPath:
/root/level1/level2/value[position() <= 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() <= 3]/text()" />
</xsl:template>
</xsl:stylesheet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With