Thomas Tempelmann
Thomas Tempelmann

Reputation: 12095

XPath: Can I collect elements by partial name match?

This is similar to How to get list of elements by partial class name?, but I like to use an XML XPath for this:

Consider this variable list of XML elements:

<root>
    <Other>...</Other>
    <Item.1>...</Item.1>
    <Item.2>...</Item.2>
    <Item.3>...</Item.3>
    <Something>...</Something>

I would like to construct an Xpath that includes all and only the Item.* elements.

I can't simply get all the parent's children, though, as there are many other siblings I do not want to have included, and I can't find a rule to exclude them all, either (it's hard to predict what else could all be in there).

There is, however, one behavior that I can rely on: The Item.* elements always start with Item.1 and are all following each other, with no other elems in between.

Is that possible with Xpath?

Upvotes: 3

Views: 2765

Answers (1)

Andersson
Andersson

Reputation: 52685

You can use below XPath expression to get elements with the names starts with Item:

//*[starts-with(local-name(), "Item")]

Upvotes: 3

Related Questions