Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match string that ends with a number using XPath

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

like image 590
Sofiane Avatar asked Oct 10 '18 14:10

Sofiane


People also ask

How do you write an XPath with an end?

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 substring in 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.

How do I combine two attributes in XPath?

The syntax for locating elements through XPath- Multiple Attribute can be written as: //<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]


1 Answers

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")))]
like image 172
Andersson Avatar answered Oct 02 '22 01:10

Andersson