Enissay
Enissay

Reputation: 4953

if condition in xPath/xQuery

To concatenate results from xpath, i'm using concat(normalize-space(.), '
') (Thanks to Dimitre Novatchev).

Now i'm wondering how to avoid adding 
 after the last element... I'm thinking about something like: concat(normalize-space(.), if(XXXXX) then '
' else '') but I couldn't figure out what condition do I have to use.

For example, If I'm using xpathExpression/concat(normalize-space(.), ',') and the values requested are (strings) A & B & C & D, the output I'm expecting is A,B,C,D instead of A,B,C,D,.... so the expression may become: xpathExpression/concat(normalize-space(.), if(this is the last element D) then '' else ',')

Upvotes: 1

Views: 689

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

If you're in XQuery or in XPath 2.0, you can use the string-join function, for example

string-join(//author, ', ')

will give you a result like

Fleming, Rowling, Pullmann

If you are in XPath 1.0, there's no easy equivalent - but it's not clear from your question exactly what the context is.

Upvotes: 1

Related Questions