开发者

Is there anything for XPATH like SQL "IN"?

开发者 https://www.devze.com 2023-02-11 17:40 出处:网络
There may be a better way of doing this and if there is please share. I have a list of ids that I would like to use to select certain elements with. For example lets say I have the following XML

There may be a better way of doing this and if there is please share. I have a list of ids that I would like to use to select certain elements with. For example lets say I have the following XML

<ANSWER calculatedValue="8917.51" 
        type="DECIMAL" 
        desc="Additional Features"  
        id="ADDTL" />
<ANSWER calculatedValue="1997.16" 
        type="DECIMAL" 
        desc="Appliances"
        id="APP" />
<ANSWER calculatedValue="6988.94" 
        type="DECIMAL" 
        desc="Electrical"
        id="ELE" />
<ANSWER calculatedValue="21184.22" 
        type="DECIMAL" 
        desc="Exterior Finish"
        id="EXT" />

I want to get the sum of the calculatedValues where the ids are ADDTL, APP and ELE. I could do this:

sum(ANSWER[@id = 'ADDTL' or @id = 'APP' or @id = 'ELE']/@calculatedValue)

Is there a way to do something like this:

sum(ANSWER[@id in ('ADDTL,APP,ELE')]/@calculatedValue)

In actual usage this list will be around 15 ids. I could probably use tokenize and index-of to do this but I was wondering if there was something like the SQL "IN" for XPATH. Thoughts, ideas. If worse comes to wors开发者_运维问答e I will just use the "or"s.


Almost. Use this XPath 1.0 expression:

sum(
  ANSWER[
    contains(
      ',ADDTL,APP,ELE,',
      concat(
        ',',
        @id,
        ','
      )
    )
  ]/@calculatedValue
)

Or this XPath 2.0 expression:

sum(ANSWER[@id = ('ADDTL','APP','ELE')]/@calculatedValue)


REF: xQuery LIKE-operator?

0

精彩评论

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