use*_*767 45 xml xpath predicate
我正在尝试构建一个复杂的xpath表达式,它将回答以下条件.
从下面的XML数据中,返回User实体,其中:
他有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>
Run Code Online (Sandbox Code Playgroud)有人可以为这种情况提供一个例子吗?
编辑:添加了复杂的配置文件实体
Gre*_*ech 70
以下应该做你想要的:
/root/user[login='user1' and
name='User 1' and
profile='admin' and
profile='operator']
Run Code Online (Sandbox Code Playgroud)
对profile
值进行两次测试可能看起来很奇怪,但由于存在多个profile
节点,因此只要至少一个节点与测试匹配,就会满足条件.
你可以比较的原因profile
直接到string
,即使它实际上是一个node
是,string-value
一个元素节点的是string-value
它的所有后代串联在一起,在这种情况下,仅仅是内容的value
.
如果profile
包含的元素多于value
您必须使用稍微复杂的谓词测试来确定是否存在匹配profile
节点value
(这应该与您更新的问题一起使用):
/root/user[login='user1' and
name='User 1' and
profile[value='admin'] and
profile[value='operator']]
Run Code Online (Sandbox Code Playgroud)
这是一个更准确的答案(目前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'
]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
74177 次 |
最近记录: |