问题描述:

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="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>
相关阅读:
Top