Remove namespaces from Xpath expression
I need a way to remove namespaces from an XPath expression, for example if it goes something like this:
/ns1:ElementOne/ElementTwo/ns2:ElementThree
he should become
/ElementOne/ElementTwo/ElementThree
Namespaces can be different in XPath (ns1, ns2 in the above example) and can be applied at the beginning of the Xpath, for example
ns3:ElementFour
Regex? Any ideas?
Thank!
source to share
The regex matches the namespace format:
[\w]+:
EDIT:
For the ancestor part, you can use the updated regex:
[\w]+:(?!:)
So the final solution is doing this: it looks for the previously declared structure with the entire alphanumeric + underscore character, followed by the ':' character, not followed by the second: character. Hope this helps.
source to share
Don't know the format of the xpath names, but the xml spec seems fuzzy to me about using colons as a namespace separator and a valid name character that should be accepted by processors. Although colons should only be used as a namespace separator.
So minus the confirmation of the unicode encoding is
To match the last colon:
[A-Za-z_:][\w:.-]*(?<=:)
To match the first colon:
(?:[A-Za-z_][\w.-]*|):
source to share