if condition in xPath/xQuery

别来无恙 提交于 2020-01-05 04:59:10

问题


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 ',')


回答1:


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.



来源:https://stackoverflow.com/questions/11755400/if-condition-in-xpath-xquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!