如何在xquery赋值中使用if else

时间:2010-09-10 18:21:06

标签: xquery if-statement assignment-operator

我正在尝试使用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。

谢谢, 索尼

1 个答案:

答案 0 :(得分:19)

那是因为在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是一个非常好的起点。