The issue is that I'm looking to construct an XPath expression to get nodes having attributes XXX having values like TP* where the star is a number. Suppose I have this XML file
<tagA attA="VAL1">text</tagA>
<tagB attB="VAL333">text</tagB>
<tagA attA="VAL2">text</tagA>
<tagA attA="V2">text</tagA>
So the xpath expression should get me all tagA
having attribute attrA
with values with the pattern VAL*
//tagA[@attrA[matches('VAL\d')]]
: is not working
We will start to write XPath with //, followed by input tag, then we will use the select attribute, followed by its attribute name like name, id, etc, and then we will choose a value of attribute in single quotes. Here, (1 of 1) means exact match. It indicates that there is only one element available for this XPath.
What is Xpath substring? In Xpath, the substring is case-sensitive which returns the function of the given input string that is executed before the first occurrence which is executed before the upcoming argument. The function has been executed in the Xpath string function list.
The syntax for locating elements through XPath- Multiple Attribute can be written as: //<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]
If you need XPath 1.0 solution, try below:
//tagA[boolean(number(substring-after(@attA, "VAL"))) or number(substring-after(@attA, "VAL")) = 0]
If @attA
cannot be "VAL0"
, then just
//tagA[boolean(number(substring-after(@attA, "VAL")))]
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