Find attribute names that start with a certain pattern

element/@*[substring(name(), 1,1) = "s"]

will match any attribute that starts with 's'.

The function starts-with() might look better than using substring()


Use:

element/@*[starts-with(name(), 's')]

This XPath expression selects all atribute nodes whose name starts with the string 's' and that are attributes of elements named element that are children of the current node.

starts-with() is a standard function in XPath 1.0


I've tested the given answers from both @Dimitre-Novatchev and @Ledhund, using lxml.html module in Python.

Both element/@*[starts-with(name(), 's')] and element/@*[substring(name(), 1,1) = "s"] return only the values of s2 and s3. You won't be able to know which value belong to which attribute.

I think in practice I would be more interested in finding the elements themselves that contain the attributes of names starting with specific characters rather than just their values.

To achieve that is very simple, just add /.. at the end,

element/@*[starts-with(name(), "s")]/..

or

element/@*[starts-with(name(), "s")]/parent::*

or

element/@*[starts-with(name(), "s")]/parent::node()

Tags:

Xpath