xml 具有多个谓词的 Xpath 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/568713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xpath expression with multiple predicates
提问by user41767
I am trying to build a complex xpath expression which will answer the following condition.
我正在尝试构建一个复杂的 xpath 表达式,它将回答以下条件。
From the XML data below, returns the Userentity which:
从下面的 XML 数据中,返回User实体,其中:
- His loginname is "user1"
- His name is "User 1"
He has 2 different profiles values which are "operator" and "admin" (I don't know the exact order ahead)
<user> <login>user1</login> <name>User 1</name> <profile> <value>admin</value> <id>2</id> <description>admin users</description> </profile> <profile> <value>operator</value> <id>1</id> <description>Operator</description> </profile> </user> <user> <login>user2</login> <name>User 2</name> <profile> <value>admin</value> <id>4</id> <description>admins users</description> </profile> <profile> <value>poweruser</value> <id>5</id> <description>power users</description> </profile> </user> </root>
- 他的登录名是“ user1”
- 他的名字是“用户1”
他有 2 个不同的配置文件值,分别是“ operator”和“ admin”(我不知道前面的确切顺序)
<user> <login>user1</login> <name>User 1</name> <profile> <value>admin</value> <id>2</id> <description>admin users</description> </profile> <profile> <value>operator</value> <id>1</id> <description>Operator</description> </profile> </user> <user> <login>user2</login> <name>User 2</name> <profile> <value>admin</value> <id>4</id> <description>admins users</description> </profile> <profile> <value>poweruser</value> <id>5</id> <description>power users</description> </profile> </user> </root>
Can someone please supply an example for such a case?
有人可以为这种情况提供一个例子吗?
EDIT: Added a complex profile entity
编辑:添加了一个复杂的配置文件实体
回答by Greg Beech
The following should do what you're after:
以下应该做你所追求的:
/root/user[login='user1' and
name='User 1' and
profile='admin' and
profile='operator']
Having two tests for the profilevalue might seem odd, but as there are multiple profilenodes then the condition will be satisfied as long as at least one node matches the test.
对该profile值进行两次测试可能看起来很奇怪,但由于有多个profile节点,因此只要至少有一个节点与测试匹配,就会满足条件。
The reason you can compare profiledirectly to a string, even though it actually is a nodeis that the string-valueof an element node is the string-valueof all its descendants concatenated together, which in this case is just the contents of value.
您可以profile直接与 a进行比较string,即使它实际上是 a 的原因node是string-value元素节点的 是string-value连接在一起的所有后代的 ,在这种情况下只是 的内容value。
If profilecontained more elements than valueyou'd have to use a slightly more complex predicate test to determine the existence of a matching profilenode based just on the value(this should work with your updated question):
如果profile包含的元素多于value您必须使用稍微复杂一点的谓词测试来确定是否存在匹配profile节点仅基于value(这应该适用于您更新的问题):
/root/user[login='user1' and
name='User 1' and
profile[value='admin'] and
profile[value='operator']]
回答by Dimitre Novatchev
Here is a more exact answer(at present Greg Beech's answer does not check for condition 3. in the problem: the userelement must have exactly 2 profilechildren):
这是一个更准确的答案(目前 Greg Beech 的答案没有检查问题中的条件 3.:user元素必须正好有 2 个profile孩子):
/*/user
[login='user1'
and
name='User 1'
and
not(profile[3])
and
profile/value='admin'
and
profile/value='operator'
]
回答by dirkgently
Assuming usersis the root:
假设users是根:
/users/user[login='user1' and name='User 1'
and (profile='admin' and profile='operator')]
回答by Ankush Jetly
/root/user[login='user1' and name='User 1' and profile/value='admin' and profile/value='operator'

