XQuery:从字符串中提取数字以用于比较

发布于 2024-10-14 00:53:54 字数 671 浏览 7 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

卖梦商人 2024-10-21 00:53:54

使用这个XPath 1.0表达式

/root/product[not(number(substring-before(size, ' ')) >= 1000)]

众所周知,XPath是XQuery的子集,所以上面也是一个XQuery表达式。

Use this XPath 1.0 expression:

/root/product[not(number(substring-before(size, ' ')) >= 1000)]

As we all know well, XPath is a subset of XQuery, so the above is also an XQuery expression.

爱的十字路口 2024-10-21 00:53:54

此 XQuery:

for $vProduct in /root/product
let $vUnits := number(substring-before($vProduct/size,'units'))
let $vSize := if ($vUnits)
              then $vUnits
              else 1000000
where $vSize le 1000
return $vProduct

输出:

<product>
    <size>500 units</size>
</product>
<product>
    <size>1000 units</size>
</product>

This XQuery:

for $vProduct in /root/product
let $vUnits := number(substring-before($vProduct/size,'units'))
let $vSize := if ($vUnits)
              then $vUnits
              else 1000000
where $vSize le 1000
return $vProduct

Output:

<product>
    <size>500 units</size>
</product>
<product>
    <size>1000 units</size>
</product>
薄暮涼年 2024-10-21 00:53:54

也许您应该使用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. The tokenize 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.

泪冰清 2024-10-21 00:53:54

为了清晰起见,稍微重复 为了

/root/product[not(substring-before(size," ") castable as xs:integer) or xs:integer(substring-before(size," ")) < 1000]

清晰起见,稍微重复

A bit of repetition for the sake of clarity

/root/product[not(substring-before(size," ") castable as xs:integer) or xs:integer(substring-before(size," ")) < 1000]

A bit of repetition for the sake of clarity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文