如何在xquery赋值中使用if else

发布于 2024-09-19 06:21:18 字数 633 浏览 2 评论 0原文

我正在尝试使用 if 条件为 xquery 中的变量赋值。我不知道该怎么做。

这就是我尝试过的:

declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $entry_type as xs:string external;
let $libx_node :=
    if ($entry_type = 'package' or 'libapp') then
      {element {fn:concat("libx:", $entry_type)} {()} }
    else if ($entry_type = 'module') then
      '<libx:module>
        <libx:body>{$module_body}</libx:body>
      </libx:module>'

此代码引发 [XPST0003] 不完整的“if”表达式错误。有人可以帮我解决这个问题吗?

另外,有人可以推荐一些学习 xqueries 的好教程吗?

谢谢, 索尼

I am trying to use a if condition to assign a value to a variable in an xquery. I am not sure how to do this.

This is what I tried:

declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $entry_type as xs:string external;
let $libx_node :=
    if ($entry_type = 'package' or 'libapp') then
      {element {fn:concat("libx:", $entry_type)} {()} }
    else if ($entry_type = 'module') then
      '<libx:module>
        <libx:body>{$module_body}</libx:body>
      </libx:module>'

This code throws an [XPST0003] Incomplete 'if' expression error. Can someone help me fix this?

Also, can someone suggest some good tutorials to learn xqueries.

Thanks,
Sony

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

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

发布评论

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

评论(1

风吹过旳痕迹 2024-09-26 06:21:18

这是因为在 XQuery 的 条件表达式规范 else-expression 始终是必需的:

[45]  IfExpr  ::=  "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle

因此您必须编写第二个 else 子句(例如,它可能返回空序列):

declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $entry_type as xs:string external;

let $libx_node :=
        if ($entry_type = ('package','libapp')) then
          element {fn:concat("libx:", $entry_type)} {()}
        else if ($entry_type = 'module') then
          <libx:module>
            <libx:body>{$module_body}</libx:body>
          </libx:module>
        else ()
... (your code here) ...

还修复了一些明显的错误:

  • 计算元素构造函数周围不需要 {} ;
  • 最有可能的是,您需要 if($entry_type = ('package', 'libapp'))

有关 XQuery 教程。 W3CSchools 的 XQuery 教程 是一个非常好的起点。

That's because in XQuery's conditional expression specification else-expression is always required:

[45]  IfExpr  ::=  "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle

So you have to write second else clause (for example, it may return empty sequence):

declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $entry_type as xs:string external;

let $libx_node :=
        if ($entry_type = ('package','libapp')) then
          element {fn:concat("libx:", $entry_type)} {()}
        else if ($entry_type = 'module') then
          <libx:module>
            <libx:body>{$module_body}</libx:body>
          </libx:module>
        else ()
... (your code here) ...

Some obvious bugs are also fixed:

  • don't need {} around computed element constructor;
  • most likely, you want if($entry_type = ('package', 'libapp'))

Concerning XQuery tutorials. The W3CSchools's XQuery Tutorial is a pretty good starting point.

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