Search code examples
xmlxpathsonarqubexpath-1.0

How to find if a sub string with XPath 1.0


I am having trouble trying to find an occurrence of a specific sub string in XPATH 1.0. This expression will be used in the code review tool SonarQube to set constraints.

This is the expression I have currently in XPATH 3.0 and I have done some research to find the conversion but it seems this expression the syntax is correct in XPATH 3.0 as it is in 1.0. Am I overlooking something or simply misinformed

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

To my understanding this uses the contains() function native to XPATH 1.0 to search any /descendant-or-self::node()/ path before it to contain the attribute example.com.


Solution

  • //*[contains(.,'@example.com')] means "find node of any type with text content that contains substring '@example.com'". If you want to "find node with attribute that contains '@example.com' in its value" you need:

    • in any attribute:

      //*[contains(@*, "@example.com")]
      
    • in specific attribute:

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

    If you want to get value of required @attribute:

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