简体   繁体   English

XQuery:如何从标签中获取特定值?

[英]XQuery: How to get a specific value out of a tag?

Suppose we have this xml:假设我们有这个 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:如果 k = "a" AND v = "1",我想返回测试的所有值 v,如下所示:

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:您可以简化并在单个 XPath 表达式中执行此操作:

//test/tag[@k = 'a' and @v = '1']/tag[not(@k = 'a' or @v = '1')]/@v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM