使用 substring-after 在 XPath 查询中搜索文本 [英] Using substring-after to search text in XPath query

查看:75
本文介绍了使用 substring-after 在 XPath 查询中搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用 XPath 查询来解析 XML 节点中的字符串,这要求我去掉一个子字符串并读取剩余的值.在我获得值之前,子字符串前后可能有动态的空格量,因此在 XPath 中使用某种 indexOf 函数会很有帮助.我正在尝试使用 substring-after,但 XPath 很有可能是 1.0,根据 这篇文章 可能会使这更难完成.以下是我尝试解析的 XML 示例:

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>

我正在尝试获取字符串中的值 7,这似乎可以通过 substring-after 和 normalize-space 的某种组合来实现.我不完全确定我是否正确使用它,因为无论我尝试以哪种方式使用它,结果要么是空值,要么是整个字符串,子字符串没有被删除.

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')]

我希望它返回'7' 谁能告诉我我在语法上做错了,或者是否有我应该使用的替代方法来完成我想要的?

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?

谢谢,

迈克

推荐答案

我能够返回整个的方式价值(两害相权取其轻)是:/myField[substring-after(.,'StringToParse')],我希望会返回7"谁能让我知道我在做什么错了

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 不是文档中的顶部元素,此表达式请求选择顶部元素,并且此元素的名称为 myField.如果针对所提供的 XML 文档进行评估,则不会选择任何节点,因为所提供的 XML 文档中的顶部元素名为 root.

  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.

substring-after() 的函数调用位于谓词内.这意味着名为myField 将根据对 substring-after() 的调用值进行选择,转换为布尔值.这显然是你想要的——你不想要一个节点——你想要一个字符串!

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!

解决方案:

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

评估为(不带引号):

"        7"

更好的解决方案可能是:

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

这评估为只是:

"7"

这篇关于使用 substring-after 在 XPath 查询中搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆