XQuery-如何删除空子元素

XQuery-如何删除空子元素,xquery,Xquery,我有以下XQuery: <a> { for $p in doc("p.xml")//p return <p q="{$p/@q}> { for $w in $p//@w where count(doc("r.xml")//s[@w = $w]) = 0 return <s w="{$w}"></s>

我有以下XQuery:

<a> {
    for $p in doc("p.xml")//p
    return
        <p q="{$p/@q}> {
            for $w in $p//@w
            where count(doc("r.xml")//s[@w = $w]) = 0
            return
                <s w="{$w}"></s>
        } </p>
} </a>
{
对于文档中的$p(“p.xml”)//p
返回

没有示例输入XML是不清楚的,但我认为,您可以使用
if-then-else
构造跳过
元素,其中
$content
将为空。类似这样的内容(未测试):

{
对于文档中的$p(“p.xml”)//p
让$content:=
在$p/@w中的$w
其中count(doc(“r.xml”)//s[@w=$w])=0
返回$w
返回
如果($content)那么

{ 在$content中输入$c 返回 }

else() }
还需要一个信息:输入用于获取结果的XML太长,无法在此处发布…非常感谢!您给了我很多启发。在

{…}

中,我只放了$content,没有if-then-else。但是在let之后,在返回之前,我放了“where count($content)>0”现在它起作用了,再次感谢你!瑞恩,你可能想考虑这个答案,或者回复你上面的评论中提到的自己的解决方案。
<a>
  <p q="p1">
    <s w="S"/>
    <s w="Sc"/>
    <s w="L"/>
  </p>
  <p q="p2"/>
  <p q="p3">
    <s w="S"/>
  </p>
</a>
 <a>
  <p q="p1">
    <s w="S"/>
    <s w="Sc"/>
    <s w="L"/>
  </p>
  <p q="p3">
    <s w="S"/>
  </p>
</a>
<a> {
    for $p in doc("p.xml")//p
    let $content := 
        for $w in $p//@w
        where count(doc("r.xml")//s[@w = $w]) = 0
        return $w
    return
        if($content) then 
            <p q="{$p/@q}"> {
                for $c in $content
                return
                    <s w="{$c}"></s>
            } </p>
        else ()
} </a>