xQuery 简单 if 非空条件

发布于 2024-10-07 21:02:39 字数 1244 浏览 0 评论 0原文

我正在尝试学习来自 php 背景的 xQuery,我有这个表达式按预期工作

<![CDATA[
declare variable $doc as node() external;
declare variable $id external;

let $cont := data($doc//div[@class="cont"])
let $title := data($doc//p[@class="vtitle"])
let $text := data($doc//div[@class="venue-cont-left"])
return 
    <venue id="{$id}">
        <title>{$title}</title>
        <text>{$text}</text>
    </venue>     
]]>  

但是现在我只想检查天气 $title 是否为空

<![CDATA[
    declare variable $doc as node() external;
    declare variable $id external;

if(empty(data($doc//p[@class="vtitle"]))) then
(
        let $id :=$id
    return  
     <venue id="{$id}" />
) else (
let $cont := data($doc//div[@class="cont"])
let $title := data($doc//p[@class="vtitle"])
let $text := data($doc//div[@class="venue-cont-left"])
return 
    <venue id="{$id}">
        <title>{$title}</title>
        <text>{$text}</text>
    </venue>
)    
]]>

这不起作用,因为我得到了这个结果输出

<venue id="4">
   <title/>
   <text>
PHONE:
ADDRESS:....

正如你所看到的场地4 没有标题,因此应该返回为

感谢您的帮助!

I'm trying to learn xQuery coming from a php background, I have this expression working as expected

<![CDATA[
declare variable $doc as node() external;
declare variable $id external;

let $cont := data($doc//div[@class="cont"])
let $title := data($doc//p[@class="vtitle"])
let $text := data($doc//div[@class="venue-cont-left"])
return 
    <venue id="{$id}">
        <title>{$title}</title>
        <text>{$text}</text>
    </venue>     
]]>  

However now I simply want to make a check weather $title is empty or not

<![CDATA[
    declare variable $doc as node() external;
    declare variable $id external;

if(empty(data($doc//p[@class="vtitle"]))) then
(
        let $id :=$id
    return  
     <venue id="{$id}" />
) else (
let $cont := data($doc//div[@class="cont"])
let $title := data($doc//p[@class="vtitle"])
let $text := data($doc//div[@class="venue-cont-left"])
return 
    <venue id="{$id}">
        <title>{$title}</title>
        <text>{$text}</text>
    </venue>
)    
]]>

This does not work because i get this resulting output

<venue id="4">
   <title/>
   <text>
PHONE:
ADDRESS:....

As you see venue 4 has no title so it should have been returned as <venue id="4" />

Thanks for any help!

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

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

发布评论

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

评论(2

谁的新欢旧爱 2024-10-14 21:02:39

使用:

declare variable $doc as node() external;
declare variable $id external;
declare variable $title := data($doc//p[@class="vtitle"]);
<venue id="{$id}">{
        if ($title)
        then <title>{$title}</title>
        else (),
        <text>{data($doc//div[@class="venue-cont-left"])}</text>
}</venue>

注意:空序列有效布尔值为假。

Use:

declare variable $doc as node() external;
declare variable $id external;
declare variable $title := data($doc//p[@class="vtitle"]);
<venue id="{$id}">{
        if ($title)
        then <title>{$title}</title>
        else (),
        <text>{data($doc//div[@class="venue-cont-left"])}</text>
}</venue>

Note: Empty sequence efective boolean value is false.

岁吢 2024-10-14 21:02:39

我只能想到一种情况导致查询失败:如果您的 XML 包含像这样没有内容的 p 节点:

<p class="vtitle" />

这样,以下代码片段将返回零长度字符串 ""(不是空序列):

data($doc//p[@class="vtitle"])

这里的问题是函数 empty() 检查空序列。因此,empty("") 返回false

如果您保留 empty() 并切换 then 和 else 表达式,您的代码应该可以工作,因为这样会处理有效布尔值 (EBV)。并且,空字符串和空序列的 EBV 均为 false

希望这是有道理的吗?

There is only one scenario I can think of where your query fails: If your XML contains a p node like this with no content:

<p class="vtitle" />

With this, the following code snippet returns a zero-length string "" (not an empty sequence):

data($doc//p[@class="vtitle"])

The problem here is that the function empty() checks for an empty sequence. Therefore, empty("") returns false.

If you would leave away the empty() and switch the then and else expressions your code should work, because then the Effective Boolean Value (EBV) is processed. And, the EBV of an empty string as well as of an empty sequence is false.

Hope that makes sense?

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