简体   繁体   中英

Filtering XML using multiple conditions of xquery

Hi I have many XML files which have the structure like:

<mydoc>
      <EC>
         <spec>
            <name>A para</name>
            <para>A</para>
            <val>40</val>
         </spec>
         <spec>
            <name>Input Voltage</name>
            <para>Vin</para>
            <val>40</val>
         </spec>
      </EC>
</mydoc>

I need to filter out only those XML files which has a spec with para=Vin and val>30

What should be my Xquery like? In DB2 I have this so far?

for $x in db2-fn:xmlcolumn('TABLE.XMLCOLUMN')/mydoc

So this would iterate over all XML files and I will have mydoc element tag in $x but now how do I implement the multiple conditions and return just the name of the spec which satisfies my conditions?

There are a couple ways to express this. XPath predicates:

for $x in db2-fn:xmlcolumn('TABLE.XMLCOLUMN')/mydoc[EC/spec[para = 'Vin' and val > 40]]

Or XQuery's where clause. This also uses XPath predicates and I think it is slightly easier to read:

for $x in db2-fn:xmlcolumn('TABLE.XMLCOLUMN')/mydoc
let $specs := $x/EC/spec
where ($spec[para = 'Vin' and val > 40])

Both should give you the same results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM