如何编写xpath来选择文本中包含撇号等特殊字符的节点?
如何编写 xpath 来选择文本中包含特殊字符(例如 ' )的节点,这在 xpath 中是无效的?
<Nodes><Node><Text>General Marketing'</Text><Nodes><Node><Text>Brochures</Text></Node><Node><Text>Value-Added</Text><Nodes><Node><Text>About Henderson</Text></Node><Node><Text>Own the World</Text></Node></Nodes></Node></Nodes></Node></Nodes>
上面的xpath
var branchName = "General Marketing'";
var xPath = String.Format("/Nodes/Node[Text = '{0}']", branchName);
失败,因为 xPath 包含以下文本:
/Nodes/Node[Text = 'General Marketing'']
如您所见,有 2 个撇号。
从我到目前为止的测试来看,只有 '.不确定是否还有其他特殊字符可能有问题?
所以我想我需要将我的 xpath 修改为其他内容。
我尝试了以下方法,但没有成功:
var xPath = String.Format("/Nodes/Node[Text = \"{0}\"]", branchName);
希望问题很清楚。
谢谢,
How to write an xpath to select a node whose text contains special characters such as ' which is kind of invalid inxpath?
<Nodes><Node><Text>General Marketing'</Text><Nodes><Node><Text>Brochures</Text></Node><Node><Text>Value-Added</Text><Nodes><Node><Text>About Henderson</Text></Node><Node><Text>Own the World</Text></Node></Nodes></Node></Nodes></Node></Nodes>
and
var branchName = "General Marketing'";
var xPath = String.Format("/Nodes/Node[Text = '{0}']", branchName);
the above xpath fails because the xPath contains the below text:
/Nodes/Node[Text = 'General Marketing'']
As you can see there are 2 apostorophes.
From my tests so far it only has problem with '. Not sure whether there are any other special characters that it may have issue with?
So I guess I'd need to modify my xpath to be something else.
I tried the below but didn't work:
var xPath = String.Format("/Nodes/Node[Text = \"{0}\"]", branchName);
Hope the question is clear.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
如果在 XSLT 中使用此 XPath 表达式,则整个 XPath 表达式可能会被撇号包围,并且必须使用内置实体 < 转义引号包围的字符串的最后一个字符(撇号) code>':
当此转换应用于提供的 XML 文档时:
选择所需元素并将其复制到输出:
这更简单比其他方法,例如
:变量
$vQ
定义为:Use:
If this XPath expression is used in XSLT, then the whole XPath expression myst be surrounded by apostrophes and the last character (apostrophe) ofthe string surrounded by quotes must be escaped using the built-in entity
'
:when this transformation is applied on the provided XML document:
the wanted element is selected and copied to the output:
This is simpler than are other approaches such as:
where the variable
$vQ
is defined as: