Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath to search for a node that has ANY attribute containing a specific string?

I can search for a String contained in an specific attribute if I use the following XPath /xs:schema/node()/descendant::node()[starts-with(@my-specific-attribute-name-here, 'my-search-string')]

However, I'd like to search for ANY attribute containing* a String

like image 575
emdog4 Avatar asked Sep 13 '11 16:09

emdog4


People also ask

How do you locate an element by partially comparing its attributes in XPath?

We can identify elements by partially comparing to its attributes in Selenium with the help of regular expression. In xpath, there is contains () method. It supports partial matching with the value of the attributes. This method comes as useful while dealing with elements having dynamic values in their attributes.

What does /* mean in XPath?

/* selects the root element, regardless of name. ./* or * selects all child elements of the context node, regardless of name.

What is local name () in XPath?

The local-name function returns a string representing the local name of the first node in a given node-set.


2 Answers

Sample XML:

<root>
  <element1 a="hello" b="world"/>
  <element2 c="world" d="hello"/>
  <element3 e="world" f="world"/>
</root>

Suppose, we need to select elements which have any attribute containing h. In this sample: element1, element2. We can use this XPath:

//*[@*[starts-with(., 'h')]]

In your sample:

/xs:schema/node()/descendant::node()
    [@*[starts-with(@my-specific-attribute-name-here, 'my-search-string')]]
like image 88
Kirill Polishchuk Avatar answered Oct 14 '22 18:10

Kirill Polishchuk


The general pattern you're looking for is:

@*[contains(., 'string')]

which will match any attribute on the context element that contains string. So if you simply want to search the whole document for attributes containing string, you'd use:

//@*[contains(., 'string')]
like image 32
Robert Rossney Avatar answered Oct 14 '22 20:10

Robert Rossney