匹配特定模式的节点名称的 XPath 查询 [英] XPath query for node names matching a certain pattern
本文介绍了匹配特定模式的节点名称的 XPath 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法将 XPath 的 starts-with
函数应用于节点的名称而不是其值?我想从以下 XML 文档中选择 FOObar
和 FOObaz
节点而不选择 notFOO
节点:
Is there a way to apply XPath's starts-with
function to a node's name instead of its value? I want to select the FOObar
and FOObaz
nodes from the following XML document without selecting the notFOO
node:
<?xml version="1.0" encoding="UTF-8" ?>
<RootNode>
<FOObar xmlns="http://sample.example.com">
<value>numOne</value>
</FOObar>
<FOObaz xmlns="http://sample.example.com">
<value>numTwo</value>
</FOObaz>
<notFOO xmlns="http://sample.example.com">
<value>numThree</value>
</notFOO>
</RootNode>
我知道可以使用 starts-with
基于文本节点进行搜索,例如
I get that it's possible to use starts-with
to search based on text nodes, e.g.
//sample:value[starts-with(.,'num')]
有没有一种方法可以写出语法上有效的以下内容?
Is there a way to write the following that is syntactically valid?
//sample:[starts-with(node(),'FOO')]
这个问题最初是用 SSCCE 提出的,但现在问题得到了回答,所有的代码都只是杂乱无章.当然,它仍然可以在修订历史记录中找到.
推荐答案
使用 name()
或 local-name()
函数按名称引用节点:
Use the name()
or local-name()
functions to refer to nodes by name:
//*[starts-with(local-name(), 'FOO')]
这篇关于匹配特定模式的节点名称的 XPath 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文