string xQuery simple if not empty 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4448800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:54:59  来源:igfitidea点击:

xQuery simple if not empty conditional

conditionalxquerystring

提问by Moak

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

我正在尝试从 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>     
]]>  

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

但是现在我只想检查天气 $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>
)    
]]>

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" />

如您所见,场地 4 没有标题,因此它应该作为 <venue id="4" />

Thanks for any help!

谢谢你的帮助!

回答by

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.

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

回答by Dennis Münkle

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

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

<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.

这里的问题是该函数empty()检查空序列。因此,empty("")返回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.

如果您放弃 the empty()and switch then 和 else 表达式,您的代码应该可以工作,因为随后会处理有效布尔值 (EBV)。并且,空字符串和空序列的 EBV 是false

Hope that makes sense?

希望这是有道理的?