XPath 谓词问题

发布于 2024-10-17 12:44:09 字数 2001 浏览 1 评论 0原文

我有一个带有选择谓词的 XPath 表达式 嵌套元素中的人名和 ID,使用 XPathNavigator.Select 方法。

这有效:

root/all_clients/client/client_name_and_ID[client_ID = 'xxx']

这也有效:

root/all_clients/client[client_name_and_ID/client_ID = 'xxx']/client_name_and_ID

当我将谓词提升到下一个级别时,它不起作用:

root/all_clients[client/client_name_and_ID/client_ID = 'xxx']/client/client_name_and_ID

我没有得到任何过滤,而是得到整个集合。

这是由于 ASP.NET 中 XPath 固有的限制吗? 或者我在做一些愚蠢的事情?

以下是相关 XML 文件的片段:

<?xml version="1.0" encoding="utf-8"?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file:///n:\Projects\XML\Medical\Example_01.xsd">

<all_clients>

    <client>
        <client_name_and_ID>
            <first_name>Fred</first_name>
            <middle_name>James</middle_name>
            <last_name>Bowman</last_name>
            <client_ID>1</client_ID>
        </client_name_and_ID>
    </client>

    <client>
        <client_name_and_ID>
            <first_name>Mark</first_name>
            <middle_name>David</middle_name>
            <last_name>Colder</last_name>
            <client_ID>2</client_ID>
        </client_name_and_ID>
    </client>

    <client>
        <client_name_and_ID>
            <first_name>Joe</first_name>
            <last_name>Lewis</last_name>
            <client_ID>3</client_ID>
        </client_name_and_ID>
    </client>

    <client>
        <client_name_and_ID>
            <first_name>Sam</first_name>
            <last_name>Plank</last_name>
            <client_ID>4</client_ID>
        </client_name_and_ID>
    </client>
</all_clients>

</root>

I've got an XPath expression with a predicate that selects
a person's name and ID from within a nested element, using
the XPathNavigator.Select method.

This works:

root/all_clients/client/client_name_and_ID[client_ID = 'xxx']

This also works:

root/all_clients/client[client_name_and_ID/client_ID = 'xxx']/client_name_and_ID

When I take the predicate to the next level, it does not work:

root/all_clients[client/client_name_and_ID/client_ID = 'xxx']/client/client_name_and_ID

I do not get any filtering, but the entire set.

Is this due to a limitation inherent within XPath, within ASP.NET,
or am I doing something stupid?

What follows is a snippet from the relevant XML file:

<?xml version="1.0" encoding="utf-8"?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="file:///n:\Projects\XML\Medical\Example_01.xsd">

<all_clients>

    <client>
        <client_name_and_ID>
            <first_name>Fred</first_name>
            <middle_name>James</middle_name>
            <last_name>Bowman</last_name>
            <client_ID>1</client_ID>
        </client_name_and_ID>
    </client>

    <client>
        <client_name_and_ID>
            <first_name>Mark</first_name>
            <middle_name>David</middle_name>
            <last_name>Colder</last_name>
            <client_ID>2</client_ID>
        </client_name_and_ID>
    </client>

    <client>
        <client_name_and_ID>
            <first_name>Joe</first_name>
            <last_name>Lewis</last_name>
            <client_ID>3</client_ID>
        </client_name_and_ID>
    </client>

    <client>
        <client_name_and_ID>
            <first_name>Sam</first_name>
            <last_name>Plank</last_name>
            <client_ID>4</client_ID>
        </client_name_and_ID>
    </client>
</all_clients>

</root>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甜宝宝 2024-10-24 12:44:09

最后一个查询匹配一个 all_clients 元素,该元素下面包含任何匹配的客户端。然后,它会在未应用过滤器的情况下选择 all_clients 下的客户端,因此它会选择所有客户端元素。

您应该将过滤器保存到 XPath 的末尾。首先选择所需的元素类型,然后对这些元素应用过滤器。尝试这些 XPath:

<!-- Select client_ID element. -->
root/all_clients/client/client_name_and_ID/client_ID[. = 'xxx']

<!-- Select client_name_and_ID element. -->
root/all_clients/client/client_name_and_ID[client_ID = 'xxx']

<!-- Select client element. -->
root/all_clients/client[client_name_and_ID/client_ID = 'xxx']

The last query is matching an all_clients element that contains any matching client underneath it. It then selects clients under this all_clients without the filter applied, so it selects all of the client elements.

You should save the filter for the end of the XPath. First select down to the type of element you want, and then apply a filter to those elements. Try these XPaths:

<!-- Select client_ID element. -->
root/all_clients/client/client_name_and_ID/client_ID[. = 'xxx']

<!-- Select client_name_and_ID element. -->
root/all_clients/client/client_name_and_ID[client_ID = 'xxx']

<!-- Select client element. -->
root/all_clients/client[client_name_and_ID/client_ID = 'xxx']
弄潮 2024-10-24 12:44:09

这很正常。所有 client 节点都是 all_clients 节点的子节点。因此,它始终符合条件“所有名为 all_clients 的元素至少有一个与后代 client/client_name_and_ID/client_ID = 'xxx' 匹配的元素”,这就是您所拥有的编码。

That's normal. All client nodes are children of the all_clients node. Therefore it will always qualify to the condition "all elements named all_clients having at least one match of a descendant client/client_name_and_ID/client_ID = 'xxx'" which is what you have coded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文