如何在xquery赋值中使用if else

son*_*ony 16 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>'
Run Code Online (Sandbox Code Playgroud)

此代码抛出[XPST0003]不完整的'if'表达式错误.有人可以帮我解决这个问题吗?

此外,有人可以建议一些很好的教程来学习xqueries.

谢谢,索尼

Shc*_*ein 19

那是因为在XQuery的条件表达式规范 中,始终需要使用else-expression:

[45]  IfExpr  ::=  "if" "(" Expr ")" "then" ExprSingle "else" ExprSingle
Run Code Online (Sandbox Code Playgroud)

所以你必须编写第二个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) ...
Run Code Online (Sandbox Code Playgroud)

一些明显的错误也是固定的:

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

关于XQuery教程.该W3CSchools的XQuery教程是一个很好的起点.