I have the following XPath to match attributes of the class span:
//span[@class='amount']
I want to match all elements that have the class attribute of "amount" but also may have other classes as well. I thought I could do this:
//span[@class='*amount*']
but that doesn't work...how can I do this?
XPATH allows the use of wildcards to write more robust path expressions where the use of specific path expressions is either impossible or undesirable. matches any element node. matches any attribute node. matches any type of node.
xml file to learn XPath wildcards and its style sheet which uses Xpath expressions to select a value from the elements. The most widely used type is the asterisk(*). To match all nodes below expression is used and the (//) operator denotes a descendant type wildcard.
There are three wildcards: * , node( ) , and @* . The * does not match attributes, text nodes, comments, or processing-instruction nodes. Thus, in the previous example, output will only come from child elements that have their own template rules that override this one.
We generally use an asterisk (*) while writing XPaths. This is called a Wildcard character. An asterisk in XPath may represent any node or attribute name of XML or DOM. A node is a tag in XML document. When we do not bother about node or attribute name we can use an asterisk.
Use the following expression:
//span[contains(concat(' ', @class, ' '), ' amount ')]
You could use contains
on its own, but that would also match classes like someamount
. Test the above expression on the following input:
<root> <span class="test amount blah"/> <span class="amount test"/> <span class="test amount"/> <span class="amount"/> <span class="someamount"/> </root>
It will select the first four span
elements, but not the last one.
You need to use contains method. See How to use XPath contains() here?
//span[contains(@class,'amount')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With