The title of this question is:
XPath expression for selecting all
nodes with a common attribute
However nowhere does the text of the question discuss how tho find all nodes that have a common attribute -- so the title may be incorrect.
To find all nodes that have a common attribute named x
(BTW, only element-nodes can have attributes), use:
//*[@x]
Use:
//@x
to select all attributes named x
in the XML document. This is probably the shortest expression to do so.
There is nothing wrong with:
//*/@x
except that it is slightly longer.
It is a shorthand for:
/descendant-or-self::node()/child::*/attribute::x
and also selects all x
attributes in the XML document.
Someone may think that this expression doesn't select the x
attribute of the top element in the document. This is a wrong conclusion, because the first location step:
/descendant-or-self::node()
selects every node in the document, including the root (/
) itself.
This means that:
/descendant-or-self::node()/child::*
selects every element, including the top element (which is the only child of the root node in a well-formed XML document).
So, when the last location step /@x
is finally added, this will select all the x
attributes of all nodes selected so far by the first two location steps -- that is all x
attributes of all element-nodes in the XML document.