gpt4 book ai didi

xml - 具有多个谓词的 Xpath 表达式

转载 作者:数据小太阳 更新时间:2023-10-29 01:37:18 30 4
gpt4 key购买 nike

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

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

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

有人可以提供这种情况的例子吗?

编辑:添加了一个复杂的配置文件实体

最佳答案

以下应该满足您的需求:

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

profile 值进行两次测试可能看起来很奇怪,但由于存在多个 profile 节点,因此只要至少有一个节点匹配测试。

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

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

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

关于xml - 具有多个谓词的 Xpath 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/568713/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com