Xpath wildcard search

陌路散爱 提交于 2019-12-11 12:52:54

问题


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.


回答1:


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"/>


来源:https://stackoverflow.com/questions/34287556/xpath-wildcard-search

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!