使用XQuery剥离所有外部命名空间节点

使用XQuery剥离所有外部命名空间节点,xquery,Xquery,输入文件: <entry xmlns="http://www.w3.org/2005/Atom"> <id>urn:uuid:1234</id> <updated>2012-01-20T11:30:11-05:00</updated> <published>2011-12-29T15:44:11-05:00</published> <link href="?id=urn:uu

输入文件:

<entry xmlns="http://www.w3.org/2005/Atom">
    <id>urn:uuid:1234</id>
    <updated>2012-01-20T11:30:11-05:00</updated>
    <published>2011-12-29T15:44:11-05:00</published>
    <link href="?id=urn:uuid:1234" rel="edit" type="application/atom+xml"/>
    <title>Title</title>
    <category scheme="http://uri/categories" term="category"/>
    <fake:fake xmlns:fake="http://fake/" attr="val"/>
    <content type="xhtml">
        <div xmlns="http://www.w3.org/1999/xhtml">
            <p>Blah</p>
        </div>
    </content>
</entry>
<!-- more entries -->

urn:uuid:1234
2012-01-20T11:30:11-05:00
2011-12-29T15:44:11-05:00
标题
废话

我希望输出完全相同,但是去掉了像
这样的非原子元素。这就是我所拥有的,根本不起作用,只是给了我同样的反馈:

declare namespace atom = "http://www.w3.org/2005/Atom";
<feed>
<title>All Posts</title>
{
for $e in collection('/db/entries')/atom:entry
return
    if
        (namespace-uri($e) = "http://www.w3.org/2005/Atom")
    then
        $e
    else
        ''
}
</feed>
声明名称空间原子=”http://www.w3.org/2005/Atom";
所有职位
{
对于集合('/db/entries')/atom:entry中的$e
返回
如果
(名称空间uri($e)=“http://www.w3.org/2005/Atom")
然后
$e
其他的
''
}

我做错了什么?

您可以尝试以下查询:

let$entry:=
urn:uuid:1234
2012-01-20T11:30:11-05:00
2011-12-29T15:44:11-05:00
标题
废话

返回{ 删除节点$entry/*[非(命名空间uri(.)=”http://www.w3.org/2005/Atom")]; $entry }
以下版本更便于携带:

let $entry := <entry xmlns="http://www.w3.org/2005/Atom">
    <id>urn:uuid:1234</id>
    <updated>2012-01-20T11:30:11-05:00</updated>
    <published>2011-12-29T15:44:11-05:00</published>
    <link href="?id=urn:uuid:1234" rel="edit" type="application/atom+xml"/>
    <title>Title</title>
    <category scheme="http://uri/categories" term="category"/>
    <fake:fake xmlns:fake="http://fake/" attr="val"/>
    <content type="xhtml">
        <div xmlns="http://www.w3.org/1999/xhtml">
            <p>Blah</p>
        </div>
    </content>
</entry>
return
  copy $new-entry := $entry
  modify (delete nodes $new-entry//*[not(namespace-uri(.) = "http://www.w3.org/2005/Atom")])
  return $new-entry
let$entry:=
urn:uuid:1234
2012-01-20T11:30:11-05:00
2011-12-29T15:44:11-05:00
标题
废话

返回 复制$new条目:=$entry 修改(删除节点$new entry/*[非(命名空间uri()=”http://www.w3.org/2005/Atom")]) 返回$newentry
有点像是一种迂回的方法,但最终还是奏效了:

declare default element namespace "http://www.w3.org/2005/Atom";
<feed>
<title>All Posts</title>
{
for $entry in collection('/db/entries')/entry
return
    element{node-name($entry)}{
        $entry/@*,
        for $child in $entry//*[namespace-uri(.) = "http://www.w3.org/2005/Atom"]
        return $child
    }
}
</feed>
声明默认元素名称空间”http://www.w3.org/2005/Atom";
所有职位
{
对于集合('/db/entries')/entry中的$entry
返回
元素{node name($entry)}{
$entry/@*,
对于$entry/*[命名空间uri(.)=”中的$childhttp://www.w3.org/2005/Atom"]
返回$child
}
}

等待时间限制到期,然后我将接受它作为回答。

谢谢,但是当输入是一个集合而不是直接嵌入时,有没有办法做到这一点?我正在尝试从eXist转换结果,但这些示例似乎并不适用于此。不管怎样,解决了它。你的例子引导我朝着正确的方向前进+1.如果您确信您的外部元素始终是entry元素的直接子元素,那么这也很好。
declare default element namespace "http://www.w3.org/2005/Atom";
<feed>
<title>All Posts</title>
{
for $entry in collection('/db/entries')/entry
return
    element{node-name($entry)}{
        $entry/@*,
        for $child in $entry//*[namespace-uri(.) = "http://www.w3.org/2005/Atom"]
        return $child
    }
}
</feed>