Using substring-after to search text in XPath query

这一生的挚爱 提交于 2020-01-04 02:56:09

问题


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


回答1:


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

You are close, but there are at least two problems with the expression:

/myField[substring-after(.,'StringToParse')]

  1. myField is not the top element in the document and this expression requests that the top element be selected and that this element's name is myField. If evaluated against the provided XML document, no node would be selected, because the top element in the provided XML document is named root.

  2. The function call to substring-after() is inside a predicate. This means that the top node named myField is to be selected depending on the value of the call to substring-after(), converted to boolean. This is clearly what you want -- you dont want a node -- you want a string!

Solution:

substring-after(/*/myField, 'StringToParse')

evaluates to (without the quotes):

"        7"

A better solution may be:

normalize-space(substring-after(/*/myField, 'StringToParse'))

this evaluates to just:

"7"


来源:https://stackoverflow.com/questions/3964642/using-substring-after-to-search-text-in-xpath-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!