Suppose we have this xml:
<question>
<test id="1">
<tag k="a" v="1"/>
<tag k="a" v="2"/>
<tag k="b" v="3"/>
</test>
<test id="2">
<tag k="a" v="1"/>
<tag k="a" v="4"/>
<tag k="b" v="5"/>
</test>
<test id="3">
<tag k="a" v="2"/>
<tag k="a" v="6"/>
<tag k="b" v="7"/>
</test>
</question>
I would like to return all values v of test, if k = "a" AND v = "1", like this:
v="3"
v="5"
So far my approach:
for $i in //test
where ($i/tag[@k = 'a' and @v = '1'])
return $i/tag/@v
But this is not correct because thats the return:
v="1"
v="2"
v="3"
v="1"
v="4"
v="5"
Thank you for your help:)
You are probably looking for something like
for $i in //test
where ($i/tag[@k = 'a'][@v="1"])
return $i/tag[@k="b"]/@v
Your criteria for selection was not exactly clear, but this returns what you expected:
for $i in //test
where $i/tag[@k = 'a' and @v = '1']
return $i/tag[not(@k = 'a' or @v = '1')]/@v
You could simplify and do this in a single XPath expression:
//test/tag[@k = 'a' and @v = '1']/tag[not(@k = 'a' or @v = '1')]/@v
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.