如何在xquery赋值中使用if else [英] how to use if else in xquery assignment

查看:177
本文介绍了如何在xquery赋值中使用if else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这就是我的尝试:

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'如果'表达错误。有人可以帮我解决这个问题吗?

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

此外,有人可以提供一些很好的教程来学习xqueries。

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

谢谢,
Sony

Thanks, Sony

推荐答案

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

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

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

所以你必须写第二个 else 子句(例如,它可能返回空序列):

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:


  • 不需要{}围绕计算元素构造函数;

  • 很可能,你想要 if($ entry_type =('package','libapp'))

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

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

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

这篇关于如何在xquery赋值中使用if else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆