15

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
  • You need to be clear what you mean by "containing". Do you mean "equal to", or "having a substring equal to"? Or, as your example suggests, "starting with"? Commented Sep 13, 2011 at 16:54

3 Answers 3

23

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')]]
Sign up to request clarification or add additional context in comments.

3 Comments

Is //*[@*[starts-with(., 'h')]] the same as //*[starts-with(@*, 'h')]?
@Eric, Nope. In 2nd XPath node-set passes to starts-with function as 1st argument (@*). The starts-with function converts a node-set to a string by returning the string value of the first node in the node-set, i.e. only 1st attribute.
Shouldn't this use the contains function?
14

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

1 Comment

Short and sweet
-1

This is what you are looking for:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.