Milind
Milind

Reputation: 493

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?

Upvotes: 2

Views: 2973

Answers (1)

wst
wst

Reputation: 11771

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.

Upvotes: 1

Related Questions