Combining multiple attribute values

Providing this type of XML file:

<data> <row val="3"/> <row val="7"/> <row val="2"/> <row val="4"/> <row val="3"/> </data> 

I need to get the string "3; 7; 2; 4; 3" using XPath 1.0 so that I can create dynamic links for the Google Chart service in an XForms application.

How can i do this? Is it possible?

+7
xpath
source share
2 answers

XPath 2.0 Solution:

 string-join(/data/row/@val,';') 

XSLT 1.0 Solution:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="row"> <xsl:value-of select="concat(substring(';',1,position()-1),@val)"/> </xsl:template> </xsl:stylesheet> 

EDIT : Shortcut XSLT 1.0.

+8
source share

Not possible in XPath (at least not in XPath 1.0, which I suppose is the version you have).

Using XSLT, this will be easy:

 <xsl:template match="/data"> <!-- select all rows for processing --> <xsl:apply-templates select="row" /> </xsl:template> <!-- rows are turned into CSV of their @val attributes --> <xsl:template match="row"> <xsl:value-of select="@val" /> <xsl:if test="position() &lt; last()"> <xsl:text>;</xsl:text> </xsl:if> </xsl:template> 

XPath is a choice language, not a processing language. You can process nodes in any other programming language that provides XML and XPath support. XSLT is just one option.

+1
source share

All Articles