具有多个谓词的 Xpath 表达式

发布于 2024-07-14 15:52:12 字数 1018 浏览 9 评论 0原文

我正在尝试构建一个复杂的 xpath 表达式,它将回答以下条件。

从下面的 XML 数据中,返回 User 实体,其中:

  1. 他的登录名是“user1
  2. 他的名字是“User 1
  3. 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:

  1. His loginname is "user1"
  2. His name is "User 1"
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秉烛思 2024-07-21 15:52:12

以下应该执行您想要的操作:

/root/user[login='user1' and 
           name='User 1' and 
           profile='admin' and
           profile='operator']

profile 值进行两次测试可能看起来很奇怪,但由于有多个 profile 节点,因此只要满足条件即可因为至少有一个节点与测试相匹配。

您可以将 profile 直接与 string 进行比较,即使它实际上是一个节点,原因是 string-value元素节点的 code> 是其所有后代连接在一起的 string-value,在本例中只是 value 的内容。

如果 profile 包含的元素多于 value,则必须使用稍微复杂的谓词测试来确定是否存在基于匹配的 profile 节点。只是在 value 上(这应该适用于您更新的问题):

/root/user[login='user1' and 
           name='User 1' and 
           profile[value='admin'] and
           profile[value='operator']]

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 profile value might seem odd, but as there are multiple profile 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 a string, even though it actually is a node is that the string-value of an element node is the string-value of all its descendants concatenated together, which in this case is just the contents of value.

If profile contained more elements than value you'd have to use a slightly more complex predicate test to determine the existence of a matching profile node based just on the value (this should work with your updated question):

/root/user[login='user1' and 
           name='User 1' and 
           profile[value='admin'] and
           profile[value='operator']]
给妤﹃绝世温柔 2024-07-21 15:52:12

这里有一个更准确的答案(目前 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'
         ]

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 2 profile children):

/*/user
        [login='user1' 
        and            
         name='User 1' 
        and  
         not(profile[3])
        and          
         profile/value='admin' 
        and           
         profile/value='operator'
         ]
铃予 2024-07-21 15:52:12

假设 users 是根:

/users/user[login='user1' and name='User 1' 
            and (profile='admin' and profile='operator')]

Assuming users is the root:

/users/user[login='user1' and name='User 1' 
            and (profile='admin' and profile='operator')]
把人绕傻吧 2024-07-21 15:52:12
/root/user[login='user1' and name='User 1' and profile/value='admin' and profile/value='operator'
/root/user[login='user1' and name='User 1' and profile/value='admin' and profile/value='operator'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文