tags:

views:

30

answers:

2

Giving this kind of XML file :

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

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

How can I do that ? Is it possible ?

A: 

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

Using XSLT, this would 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 selection language, not a processing language. You can process the nodes in any other programming language that provides XML and XPath support - XSLT is only one of the options.

Tomalak
+2  A: 

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"&gt;
    <xsl:output method="text"/>
    <xsl:template match="row">
        <xsl:value-of select="concat(substring(';',1,position()-1),@val)"/>
    </xsl:template>
</xsl:stylesheet>

EDIT: Short XSLT 1.0 solution.

Alejandro
+1 for the `concat()` trick. :-)
Tomalak