如何使用 XPath 1.0 查找子字符串

How to find if a sub string with XPath 1.0

我在尝试在 XPATH 1.0 中查找特定子字符串时遇到问题。该表达式将在代码审查工具SonarQube中用于设置约束。

这是我目前在 XPATH 3.0 中使用的表达式,我已经进行了一些研究以找到转换,但看起来这个表达式的语法在 XPATH 3.0 中是正确的,就像在 1.0 中一样。我是忽略了什么还是只是被误导了

//*[contains(.,'@example.com')]

据我了解,这使用 XPATH 1.0 原生的 contains() 函数来搜索它之前的任何 /descendant-or-self::node()/ 路径以包含属性 example.com

//*[contains(.,'@example.com')]表示"find node of any type with text content that contains substring '@example.com'"。如果你想 "find node with attribute that contains '@example.com' in its value" 你需要:

  • 任意属性中:

    //*[contains(@*, "@example.com")]
    
  • 具体属性中:

    //*[contains(@email, "@example.com")]
    

如果你想获得所需的值 @attribute:

//*[contains(@email, "@example.com")]/@email