I trying to parse a string in an XML node by using an XPath query which requires that I strip out a substring and read the remaining value. The substring may have a dynamic amount of whitespace before and after it before I can get to the value, so it would be helpful to have some sort of a indexOf function to use in the XPath. I’m trying to use substring-after, but there is a good chance the XPath is 1.0, which according to this post might make this more difficult to accomplish. The following is an example of the XML I am trying to parse:
<root>
<myField> StringToParse 7</myField>
</root>
I’m trying to get at the value 7 in the string, which would seem possible with some combination of substring-after and normalize-space. I’m not entirely sure I’m using it correctly because no matter which way I try to use it, the result is either a null value or the entire string, with the substring not removed.
The way I am able to return the entire value (the lesser of two evils) is:
/myField[substring-after(.,'StringToParse')]
which I would hope would return ‘ 7’ Could anyone let me know I’m doing wrong with the syntax, or if there is an alternate method I should be using to accomplish what I want?
Thanks,
Mike
You are close, but there are at least two problems with the expression:
/myField[substring-after(.,'StringToParse')]myFieldis not the top element in the document and this expression requests that the top element be selected and that this element’s name ismyField. If evaluated against the provided XML document, no node would be selected, because the top element in the provided XML document is namedroot.The function call to
substring-after()is inside a predicate. This means that the top node namedmyFieldis to be selected depending on the value of the call tosubstring-after(), converted to boolean. This is clearly what you want — you dont want a node — you want a string!Solution:
evaluates to (without the quotes):
A better solution may be:
this evaluates to just: