【问题标题】:how to use if else in xquery assignment如何在 xquery 赋值中使用 if else
【发布时间】:2011-04-10 21:00:20
【问题描述】:

我正在尝试使用 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] Incomplete 'if' 表达式错误。有人可以帮我解决这个问题吗?

另外,有人可以推荐一些好的教程来学习 xquery。

谢谢, 索尼

【问题讨论】:

    标签: xquery if-statement assignment-operator


    【解决方案1】:

    这是因为在 XQuery 的 conditional expression specification 中总是需要 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's XQuery Tutorial 是一个很好的起点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      相关资源
      最近更新 更多