developer
developer

Reputation: 1675

Xpath wildcard search

I want to search within all nodes and all attributes for a specific text using xpath query.

Example:

<Employee>
  <Id>1</Id>
  <Info FirstName="Search" LastName="Smith">
    <Address City="Search"/>
  </Info>
</Employee>

I am currently using the below xpath query:

var nodes = xmlConfig.SelectNodes("//*[contains(@*,'Search')]");

Xpath query should return Info node and Address node. However, it only returns one node.

Upvotes: 2

Views: 7348

Answers (1)

har07
har07

Reputation: 89285

Assuming that you're using XPath 1.0, your XPath should have returned Info node and Address node as you expect. See online demo for that in the following link (click on 'test' button to execute the XPath) :

//*[contains(@*,'Search')]

xpathtester xpath 1.0 demo

In XPath 2.0, the above will throw exception passing sequence of more than one item is not allowed as the first argument of contains(), and that happen because one element may have more than one attribute. For XPath 2.0 compatible expression, you can do this way instead :

//*[@*[contains(.,'Search')]]

xpathtester xpath 2.0 demo

output :

<Info FirstName="Search" LastName="Smith">
    <Address City="Search"/>
  </Info>

<Address City="Search"/>

Upvotes: 4

Related Questions