开发者

Excluding elements in XQuery

I have an XML element such as:

<items>
   <item>
      <size>360</size>
      <quantity>400</quantity>
   <item>
   <item>
      <size>540</size>
      <quantity>1200</quantity>
   <item>
   <item>
      <size>360</size>
      <quantity>600</quantity>
   <item>
   <item>
      <size>800</size>
      <quantity>750</quantity>
   <item>
</items>

What I need to do is iterate over this element and pull out ONLY the first item el开发者_如何学JAVAements with a distinct size element. So I want to have:

<item>
  <size>360</size>
  <quantity>400</quantity>
<item>
<item>
   <size>540</size>
   <quantity>1200</quantity>
<item>
<item>
   <size>800</size>
   <quantity>750</quantity>
<item>

Removing the second item element with a size of 360. Is there a way of filtering these out in a for loop filter statement?


One way is:

for $item in $items
where $item is $items[size = $item/size][1]
return $item

another is

let $sizes = distinct-values($items/size)
for $size in $sizes
return $items[size = $size][1]

and yet another (more evil) way is

$items[index-of($items/size, size)[1]]


Is the following query helping?

for $item in $items
let $size := number($item/size/text()) 
return
  if($size != 360) then
    $item
  else ()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜