开发者

Concatenate multiple attribute values

开发者 https://www.devze.com 2023-01-09 21:17 出处:网络
Giving this kind of XML file : <data> <row val=\"3\"/> <row val=\"7\"/> <row val=\"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 ?


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: Short XSLT 1.0 solution.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号