Christian68
Christian68

Reputation: 987

How to concatenate XML elements using XPath

I have the following XML structure:

<list>
   <value>
      <firstname>mark1</firstname>
      <address>my address</address>
      <city>bruxelles</city>
      <id>1</id>
      <lastname>durand</lastname>
   </value>
   <value>
      <firstname>mark2</firstname>
      <address>my address</address>
      <city>bruxelles</city>
      <id>2</id>
      <lastname>durand</lastname>
   </value>
   <value>
      <firstname>mark3</firstname>
      <address>my address</address>
      <city>bruxelles</city>
      <id>3</id>
      <lastname>durand</lastname>
   </value>
</list>

When I do /list/value/firstname/text(), I have:

mark1
mark2
mark3

As I would like to have something like

1 - mark1
2 - mark2
3 - mark3

I tried: concat(/list/value/id/text(),"-", /list/value/firstname/text())

But it would only return the first set, i.e.

1-mark1

Is there a way to achieve what I'm looking for and loop over the nodes?

Upvotes: 0

Views: 370

Answers (1)

kjhughes
kjhughes

Reputation: 111786

XPath 1.0

Not possible without assistance of hosting language.

XPath 2.0

This XPath,

/list/value/concat(firstname, ' - ', id)

will return

1 - mark1
2 - mark2
3 - mark3

as requested.

Upvotes: 1

Related Questions