XPath 谓词问题
我有一个带有选择谓词的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后一个查询匹配一个 all_clients 元素,该元素下面包含任何匹配的客户端。然后,它会在未应用过滤器的情况下选择 all_clients 下的客户端,因此它会选择所有客户端元素。
您应该将过滤器保存到 XPath 的末尾。首先选择所需的元素类型,然后对这些元素应用过滤器。尝试这些 XPath:
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:
这很正常。所有
client
节点都是all_clients
节点的子节点。因此,它始终符合条件“所有名为all_clients
的元素至少有一个与后代client/client_name_and_ID/client_ID = 'xxx'
匹配的元素”,这就是您所拥有的编码。That's normal. All
client
nodes are children of theall_clients
node. Therefore it will always qualify to the condition "all elements namedall_clients
having at least one match of a descendantclient/client_name_and_ID/client_ID = 'xxx'
" which is what you have coded.