XQuery:从字符串中提取数字以用于比较
我对 XQuery 非常陌生,坦率地说,我发现学习曲线非常陡峭。
我有一个看起来像这样的 XML 结构:
<root>
<product>
<size>500 units</size>
</product>
<product>
<size>1000 units</size>
</product>
<product>
<size>Unlimited units</size>
</product>
</root>
我需要编写一个 XQuery 语句,返回 size 中的数值小于 1000 的所有节点。所以我需要以某种方式确定这个数值(忽略任何文本)执行我假设的“le”操作。
除此之外,节点有可能根本没有数字(例如“无限单位”),在这种情况下,需要将其视为具有 1000000 的值。
有什么方法可以做到这一点吗?我尝试了 fn:replace(blah, '\D', '') 和强制转换为 xs:int 的各种组合,但我似乎无法让它工作。
任何指导将不胜感激。
I am very new to XQuery, and frankly find the learning curve incredibly steep.
I have an XML structure that looks something like this:
<root>
<product>
<size>500 units</size>
</product>
<product>
<size>1000 units</size>
</product>
<product>
<size>Unlimited units</size>
</product>
</root>
I need to write an XQuery statement that returns all nodes where the numerical value in size is less than say 1000. So I somehow need to determine this numerical value (ignoring any text) to perform an 'le' operation I assume.
On top of this, there is the possibility that the node will have no digits at all (e.g. 'Unlimited units'), in which case it needs to be treated as having a value of say 1000000.
Is there some way to do this? I've tried various combinations of fn:replace(blah, '\D', '') and casting to xs:int, but I can't seem to get it to work.
Any guidance would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用这个XPath 1.0表达式:
众所周知,XPath是XQuery的子集,所以上面也是一个XQuery表达式。
Use this XPath 1.0 expression:
As we all know well, XPath is a subset of XQuery, so the above is also an XQuery expression.
此 XQuery:
输出:
This XQuery:
Output:
也许您应该使用
fn:tokenize(subject,pattern,flags)
(这里是tokenize) 函数来提取您的 int 值。 tokenize 函数将返回一个数组,其中 int 值位于数组的第一个位置。然后你可以用你的 int 值做任何你想做的事情。为了区分给定单位的情况和“无限单位”的情况,您可以使用“无限单位”执行正则表达式
fn:match
或在标记化之前简单地比较字符串。如果字符串是“无限单位”,则您认为是必要的,而在其他情况下,您对字符串进行标记以提取 int 值。Probably you should use the
fn:tokenize(subject, pattern, flags)
(here is an description of the tokenize) function to extract your int values. Thetokenize
function will return an array with your int values in the first position of the array. You can then do whatever you want with your int values.To distinguish between the cases where the units are given and the "Unlimited units" case, you could do a regexp
fn:match
with "Unlimited units" or simply compare the string before tokenizing. If the string is "Unlimited units" you'd treat is ar necessary and in the other case you tokenize the string to extract the int value.为了清晰起见,稍微重复 为了
清晰起见,稍微重复
A bit of repetition for the sake of clarity
A bit of repetition for the sake of clarity