带有not()和ends-with()的Xpath错误 [英] Xpath error with not() and ends-with()

查看:152
本文介绍了带有not()和ends-with()的Xpath错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Xpath表达式:

I have the following Xpath expression:

//*[not(input)][ends-with(@*, 'Copyright')]

我希望它能给我所有元素 - 输入除外 - 具有任何属性值以Copyright结尾。

I expect it to give me all elements - except input - with any attribute value which ends with "Copyright".

我在Selenium 2 Java API中使用 webDriver.findElements(By.xpath(expression))执行它并收到以下错误:

I execute it in the Selenium 2 Java API with webDriver.findElements(By.xpath(expression)) and get the following error:


表达式不是合法的
表达式

The expression is not a legal expression

但这些表达式没有问题:

But these expressions work without trouble:

//*[not(input)][starts-with(@*, 'Copyright')]
//*[ends-with(@*, 'Copyright')]

任何想法?

推荐答案


我有以下Xpath表达式:

I have the following Xpath expression:

//*[not(input)][ends-with(@*, 'Copyright')]

我希望它能给我所有元素 -
除输入外 - 任何属性
值以Copyright结尾。

I expect it to give me all elements - except input - with any attribute value which ends with "Copyright".

有这里有几个问题


  1. ends-with()只是一个标准的XPath 2.0函数,所以你可能正在使用XPath 1.0引擎并且它正确引发错误,因为它不知道一个名为的函数ends-with()

  1. ends-with() is a standard XPath 2.0 function only, so the chances are you are using an XPath 1.0 engine and it correctly raises an error because it doesn't know about a function called ends-with().

即使您使用的是XPath 2.0处理器,表达式也会结束(@ *, 'Copyright')在一般情况下导致错误,因为 ends-with()函数被定义为接受最多一个字符串( xs:string?)作为两个操作数 - 但是 @ * 会产生一个包含多个字符串的序列元素具有多个属性的情况。

Even if you are working with an XPath 2.0 processor, the expression ends-with(@*, 'Copyright') results in error in the general case, because the ends-with() function is defined to accept atmost a single string (xs:string?) as both of its operands -- however @* produces a sequence of more than one string in the case when the element has more than one attribute.

// * [not(input)] 并不意味着选择所有未命名的元素输入。真正的含义是:选择所有没有名为input的子元素的元素。

//*[not(input)] doesn't mean "select all elements that are not named input. The real meaning is: "Select all elements that dont have a child element named "input".

解决方案


  1. 使用此XPath 2.0表达式: // * [not(self :: input)] [@ * [ends-with(。,'Copyright')]]

如果是XPath 1.0使用以下表达式:

In the case of XPath 1.0 use this expression:

....

  //*[not(self::input)]
        [@*[substring(., string-length() -8) = 'Copyright']]

以下是使用XSLT对上一个XPath表达式的简短而完整的验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <xsl:copy-of select=
     "//*[not(self::input)]
           [@*[substring(., string-length() -8)
              = 'Copyright'
              ]
          ]"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下XML文档时:

<html>
 <input/>
 <a x="Copyright not"/>
 <a y="This is a Copyright"/>
</html>

生成所需的正确结果

<a y="This is a Copyright"/>

如果XML文档位于默认命名空间

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://www.w3.org/1999/xhtml"
 >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <xsl:copy-of select=
     "//*[not(self::x:input)]
           [@*[substring(., string-length() -8)
              = 'Copyright'
              ]
          ]"/>
 </xsl:template>
</xsl:stylesheet>

<html xmlns="http://www.w3.org/1999/xhtml">
 <input z="This is a Copyright"/>
 <a x="Copyright not"/>
 <a y="This is a Copyright"/>
</html>

产生了想要的正确结果:

<a xmlns="http://www.w3.org/1999/xhtml" y="This is a Copyright"/>

这篇关于带有not()和ends-with()的Xpath错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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