I can't find an explanation on the following. I've made this test script in order to solve my previous question.
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 := <xml>
<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>
</xml>
Then, the following using 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'
returns as expected :
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
Whereas the following using 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'
returns as not expected :
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
Any explanation ? Why Conditional Expressions would not behave the same way than Switch Expressions ?
Aucun commentaire:
Enregistrer un commentaire