开发者

XQuery to filter dynamic list of rows

I'm looking for some guidance to get me moving in the right direction. I am using xquery to return an xml document that looks similar to the xml below.

<myDoc>
  <myElements id="1">
    <myElement key="one">aaa</myElement>
    <myElement key="two" >bbb</myElement>
    <myElement key="three">ccc</myElement>
  </myElements>
  <myElements id="2">
    <myElement key="one">ddd</myElement>
    <myElement key="two" >eee</myElement>
    <myElement key="three">fff</myElement>
  </myElements>
</myDoc>

I am trying to return the doc with only the specific <myElements> stanzas whose key is specified. For example, if the keys "one" and "three" are specified, the resulting xml should look like:

<myDoc>
  <myElements id="1">
    <myElement key开发者_如何学JAVA="one">aaa</myElement>
    <myElement key="three">ccc</myElement>
  </myElements>
  <myElements id="2">
    <myElement key="one">ddd</myElement>
    <myElement key="three">fff</myElement>
  </myElements>
</myDoc>

Is this feasible? any advice that could point me in the right direction would be much appreciated.


Problems of this kind are much more suited for XSLT, but it is also possible to solve them in XQuery.

Something like this:

let $vKeys := ('one', 'three')
 return
   element {name(/*)}
     {for $child in /*/*
       return
        element {name($child)} 
          {for $att in $child/@*
            return
             attribute {name($att)} {$att},
           for $grandchild in $child/myElement[@key=$vKeys]
             return $grandchild
          }
     }

when this query is executed against the provided XML document:

<myDoc>
  <myElements id="1">
    <myElement key="one">aaa</myElement>
    <myElement key="two" >bbb</myElement>
    <myElement key="three">ccc</myElement>
  </myElements>
  <myElements id="2">
    <myElement key="one">ddd</myElement>
    <myElement key="two" >eee</myElement>
    <myElement key="three">fff</myElement>
  </myElements>
</myDoc>

the wanted, correct result is produced:

<myDoc>
   <myElements id="1">
      <myElement key="one">aaa</myElement>
      <myElement key="three">ccc</myElement>
   </myElements>
   <myElements id="2">
      <myElement key="one">ddd</myElement>
      <myElement key="three">fff</myElement>
   </myElements>
</myDoc>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜