如何使用XPath选择包含单引号的元素? [英] How to select element that contains single quotes using XPath?

查看:135
本文介绍了如何使用XPath选择包含单引号的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 XPath 选择此元素:

<a href="#" onClick="onViewDocument('2016', '1');">2016</a>

到目前为止,我有这样的:

So far I have this:

//a[@onClick='onViewDocument('2016', '1');']

我需要绕过 2016 1

Do I need to escape the single quotes around the 2016 and 1?

推荐答案

最简单的是使用',具体取决于字符串文字中已经使用的内容。

Simplest usually is to use the alternative of ' or ", depending upon what was already used surrounding the string literal.

如果不可行,另一种方法是使用<$ '单引号):

If that's not feasible, an alternative is to use &apos; for ' (single quote):

//a[@onClick='onViewDocument(&apos;2016&apos;, &apos;1&apos;);']

请注意,您可以使用& quot; code>(双引号)。

Note that you can use &quot; for " (double quote).

参考 strong> XML路径语言(XPath)版本1.0

Reference: XML Path Language (XPath) Version 1.0:


XPath表达式经常出现在XML属性中。本节中指定的
的语法适用于XML 1.0
规范化后的属性值。所以,例如,如果语法使用字符<
这不能出现在XML源中,因为< 但必须根据XML 1.0规则引用
,例如,将其输入为& lt;
在表达式中,文字字符串由单引号或双引号
引号分隔,也用于分隔XML属性。要
避免在XML
处理器解释的表达式中的引号作为终止属性值,引号可以作为字符引用输入
& &'/ code>)。或者,
如果XML属性是以双引号分隔的
,则表达式可以使用单引号。反之亦然。

XPath expressions often occur in XML attributes. The grammar specified in this section applies to the attribute value after XML 1.0 normalization. So, for example, if the grammar uses the character <, this must not appear in the XML source as < but must be quoted according to XML 1.0 rules by, for example, entering it as &lt;. Within expressions, literal strings are delimited by single or double quotation marks, which are also used to delimit XML attributes. To avoid a quotation mark in an expression being interpreted by the XML processor as terminating the attribute value the quotation mark can be entered as a character reference (&quot; or &apos;). Alternatively, the expression can use single quotation marks if the XML attribute is delimited with double quotation marks or vice-versa.

这篇关于如何使用XPath选择包含单引号的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆