如何在 XQuery 中打开字符串?

发布于 2024-07-04 09:18:56 字数 67 浏览 6 评论 0原文

我有一个作为字符串输入的外部变量,我想对其进行 switch/case 操作。 我如何在 xquery 中做到这一点?

I have an external variable coming in as a string and I would like to do a switch/case on it. How do I do that in xquery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

挽清梦 2024-07-11 09:18:56

只需使用一系列 if 表达式:

if ($room eq "bathroom") then "loo"
else if ($room eq "kitchen")  then "scullery"
else "just a room"

使用类型开关会隐藏您真正在做什么。

其中哪种方法最有效取决于您所使用的 XQuery 处理器。 在理想的情况下,这应该只是一个品味问题,因为应该由优化器来选择合适的方法,但如果性能很重要,则值得对两个版本进行基准测试。 如果处理器优化了您的示例中的节点结构,并且没有将我的示例优化为专用交换机,我会感到非常惊讶。

Just use a series of if expressions:

if ($room eq "bathroom") then "loo"
else if ($room eq "kitchen")  then "scullery"
else "just a room"

Using a typeswitch is hiding what you are really doing.

Which of these methods is most efficient will depend on the XQuery processor you are using. In an ideal world it should only be a matter of taste, as it should be down to the optimizer to select the appropriate method, but if performance is important it is worth benchmarking both versions. I would be very surprised if a processor optimized the node construction out of your example, and didn't optimize my example to a specialized switch.

暖阳 2024-07-11 09:18:56

XQuery 没有用于切换除元素以外的任何内容的功能。

您要做的第一件事是将字符串转换为元素。

let $str := "kitchen"
let $room := element {$str} {}

然后只需使用 typeswitch 进行普通切换:

return typeswitch($room)
  case element(bathroom) return "loo"
  case element(kitchen) return "scullery"
  default return "just a room"

请注意,这可能是 MarkLogic 唯一的解决方案。

XQuery doesn't have a function for switching on anything other than elements.

The first thing you do is convert your string to an element.

let $str := "kitchen"
let $room := element {$str} {}

Then just use typeswitch to do a normal switch:

return typeswitch($room)
  case element(bathroom) return "loo"
  case element(kitchen) return "scullery"
  default return "just a room"

Please note, this may be a MarkLogic only solution.

淡写薰衣草的香 2024-07-11 09:18:56

从 XQuery 1.1 开始,使用开关:

http://www.w3.org/ TR/xquery-11/#id-switch

switch ($animal) 
   case "Cow" return "Moo"
   case "Cat" return "Meow"
   case "Duck" return "Quack"
   default return "What's that odd noise?" 

Starting with XQuery 1.1, use switch:

http://www.w3.org/TR/xquery-11/#id-switch

switch ($animal) 
   case "Cow" return "Moo"
   case "Cat" return "Meow"
   case "Duck" return "Quack"
   default return "What's that odd noise?" 
执笔绘流年 2024-07-11 09:18:56

对于 Saxon,您可以使用如下内容:

declare function a:fn($i) {
typeswitch ($i)
 case element(a:elemen1, xs:untyped) return 'a' 
 case element(a:elemen2, xs:untyped) return 'b' 
 default return "error;"
};

https://rrusin。 blogspot.com/2010/01/xquery4j-in-action.html

For Saxon, you can use something like this:

declare function a:fn($i) {
typeswitch ($i)
 case element(a:elemen1, xs:untyped) return 'a' 
 case element(a:elemen2, xs:untyped) return 'b' 
 default return "error;"
};

https://rrusin.blogspot.com/2010/01/xquery4j-in-action.html

夜访吸血鬼 2024-07-11 09:18:56

如果您的处理器支持 XQuery 1.1,那么您可以简单地执行以下操作:

switch ($room) 
  case "bathroom" return "loo"
  case "kitchen" return "scullery"
  default return "just a room"

If your processor supports XQuery 1.1, then you can simply do:

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