Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove namespaces from Xpath expression

Tags:

regex

xpath

I need a way to remove namespaces from an XPath expression, e.g if it's something like this:

/ns1:ElementOne/ElementTwo/ns2:ElementThree

it should become

/ElementOne/ElementTwo/ElementThree

Namespaces could differ within the XPath (ns1, ns2 in the example above) and can be applied at the beginning of the Xpath, e.g.

ns3:ElementFour

Regex? Any ideas?

Thanks!

like image 281
Yuriy Galanter Avatar asked Nov 16 '25 02:11

Yuriy Galanter


1 Answers

The regex bellow matches the namespace format:

[\w]+:

EDIT:

For the ancestor part you can use the updated regex:

[\w]+:(?!:)

So the final solution does the following, It searches for the previously stated structure with all alphanumerical + underscore string format followed by a ':' character which is not followed by a second ':' character. Hope this helps.

like image 72
Bogdan Emil Mariesan Avatar answered Nov 17 '25 20:11

Bogdan Emil Mariesan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!