Reputation: 217
example i have xml data like this
<a>
<book id="bk1">
<test>Yeah</test>
</book>
<book id="bk2">
<test>Hellow</test>
</book>
<book id="bk3">
<test>yaya</test>
<test>love</test>
<test>hello</test>
</book>
</a>
my question is using xquery to select the book id where test element doesn't contain any value="love"
expected output:
<book id="bk1"/>
<book id="bk2"/>
but my output is like :
<book id="bk1"/>
<book id="bk2"/>
<book id="bk3"/>
following code:
for $b in /a
let $i := $b/book/test
let $id := $b/book/@id
where $i != "love"
return <book id="@id"/>
i suspect it's go to looping for bk3 and found another test value which is not love and return it. how to solve it?
Upvotes: 0
Views: 1510
Reputation: 38732
You code is returning a single, empty element for me. Instead of looping over the "container" <a/>
node, loop over the books and filter as needed.
for $book in /a/book
where not($book/test = "love")
return <book>{ $book/@id }</book>
A second problem was with the @id
attribute: To access the variable during cunstruction, you have to put curly brackets around it and access the variable, not some attribute without giving a context: <book id="{ $id }"/>
. I used a slightly different way of constructing the element by including the attribute from the original element instead of reconstructing it.
Edit: Bonus one liner for XQuery 3.0 capable systems:
/a/book[not($book/test = "love")]/element book { ./@id }
Upvotes: 1