具有多个谓词的 Xpath 表达式
我正在尝试构建一个复杂的 xpath 表达式,它将回答以下条件。
从下面的 XML 数据中,返回 User 实体,其中:
- 他的登录名是“user1”
- 他的名字是“User 1”
He有 2 个不同的配置文件值,分别是“operator”和“admin”(我不知道前面的确切顺序)
<预><代码><用户>; <登录>用户1 <名称>用户 1 <简介> <值>管理2 <描述>管理员用户 <简介> <值>运算符1 <描述>运算符 <用户> <登录>用户2 <名称>用户2 <简介> <值>管理4 <描述>管理员用户 <简介> <值>超级用户5 <描述>高级用户
有人可以提供这种情况的例子吗?
编辑:添加了复杂的配置文件实体
I am trying to build a complex xpath expression which will answer the following condition.
From the XML data below, returns the User entity which:
- 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>
Can someone please supply an example for such a case?
EDIT: Added a complex profile entity
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下应该执行您想要的操作:
对
profile
值进行两次测试可能看起来很奇怪,但由于有多个profile
节点,因此只要满足条件即可因为至少有一个节点与测试相匹配。您可以将
profile
直接与string
进行比较,即使它实际上是一个节点
,原因是string-value
元素节点的 code> 是其所有后代连接在一起的string-value
,在本例中只是value
的内容。如果
profile
包含的元素多于value
,则必须使用稍微复杂的谓词测试来确定是否存在基于匹配的profile
节点。只是在value
上(这应该适用于您更新的问题):The following should do what you're after:
Having two tests for the
profile
value might seem odd, but as there are multipleprofile
nodes then the condition will be satisfied as long as at least one node matches the test.The reason you can compare
profile
directly to astring
, even though it actually is anode
is that thestring-value
of an element node is thestring-value
of all its descendants concatenated together, which in this case is just the contents ofvalue
.If
profile
contained more elements thanvalue
you'd have to use a slightly more complex predicate test to determine the existence of a matchingprofile
node based just on thevalue
(this should work with your updated question):这里有一个更准确的答案(目前 Greg Beech 的答案不检查问题中的条件 3:
user
元素必须恰好有 2 个profile孩子):
Here is a more exact answer (at present Greg Beech's answer does not check for condition 3. in the problem: the
user
element must have exactly 2profile
children):假设
users
是根:Assuming
users
is the root: