我找不到关于以下内容的解释。我制作了这个测试脚本来解决我之前的问题。
xquery version "3.0" ;
declare default function namespace 'local' ;
declare function local:is-img-only( $element as element() ) as xs:boolean {
($element/child::*[1] instance of element(img))
and (fn:not($element/child::*[2]))
and (fn:normalize-space($element) = '')
} ;
let $in-xml := <myxml>
<p id="1">
<img id="1"/>
</p>
<p id="2">
<img id="1"/>
hello
</p>
<p id="3">
<img id="1"/>
</p>
<p id="4">
<blockquote>hello</blockquote>
<img id="1"/>
</p>
<p id="5">
<img id="1"/>
<img id="2"/>
</p>
</myxml>
然后,以下使用if then else
:
for $p in $in-xml/p
return if (local:is-img-only($p))
then $p/@id/fn:data() || ' has only an img child'
else $p/@id/fn:data() || ' has not strictly an img child'
按预期返回:
1 has only an img child
2 has not strictly an img child
3 has only an img child
4 has not strictly an img child
5 has not strictly an img child
而以下使用switch case
for $p in $in-xml/p
return switch ($p)
case (local:is-img-only($p)) return $p/@id/fn:data() || ' has only an img child'
default return $p/@id/fn:data() || ' has not strictly an img child'
未按预期返回:
1 has not strictly an img child
2 has not strictly an img child
3 has not strictly an img child
4 has not strictly an img child
5 has not strictly an img child
有什么解释吗?为什么Conditional Expressions的行为方式与Switch Expressions不同?