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

喜夏-厌秋 提交于 2019-12-29 05:57:13

问题


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


回答1:


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')]]



回答2:


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')]


来源:https://stackoverflow.com/questions/7405212/xpath-to-search-for-a-node-that-has-any-attribute-containing-a-specific-string

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