更改 XQuery 中的命名空间
我在 O'Reiily 书籍 XQuery 中找到了以下 XQuery 用户定义函数,它用于在打印 XML 文件时更改 XML 文件的命名空间:
declare namespace functx = "http://www.functx.com";
declare function functx:change-element-ns-deep
($element as element(), $newns as xs:string) as element()
{
let $newName := QName ($newns, name ($element))
return (element {$newName}
{$element/@*,
for $child in $element/node()
return if ($child instance of element())
then functx:change-element-ns-deep ($child, $newns)
else $child
}
)
};
调用此函数的一个示例是:
<text xmlns:pre="pre">
{
functx:change-element-ns-deep(<pre:x><pre:y>123</pre:y></pre:x>, "http://new")
}
</text>
returns :
<test xmlns:pre="pre" >
< x xmlns="http//new">
<y>123</y>
</x>
</test>
但我得到的是:
<test>
<x>
<y>123</y>
</x>
</test>
原始命名空间似乎已被删除,但新命名空间尚未附加或 难道只是处理器不打印名称空间,因为未受影响的名称空间声明也消失了?
I found the following XQuery user-defined function in the O'Reiily book XQuery and it is used to change the namespace of an XML file when it is printed:
declare namespace functx = "http://www.functx.com";
declare function functx:change-element-ns-deep
($element as element(), $newns as xs:string) as element()
{
let $newName := QName ($newns, name ($element))
return (element {$newName}
{$element/@*,
for $child in $element/node()
return if ($child instance of element())
then functx:change-element-ns-deep ($child, $newns)
else $child
}
)
};
One example to call this function with is:
<text xmlns:pre="pre">
{
functx:change-element-ns-deep(<pre:x><pre:y>123</pre:y></pre:x>, "http://new")
}
</text>
returns:
<test xmlns:pre="pre" >
< x xmlns="http//new">
<y>123</y>
</x>
</test>
But what I've got is:
<test>
<x>
<y>123</y>
</x>
</test>
It appears that the original namespace is stripped but the new one has yet to be attached or
is it simply that the processor doesn't print the namespace because the unaffected namespace declaration is also gone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遗憾的是,eXist Sandbox 结果窗口不显示名称空间属性 (@xmlns)。但是,如果您将查询保存为 .xq 文件并通过浏览器运行它,您会发现它实际上正确保留了命名空间信息。顺便说一句,下一代版本的 Sandbox(称为 eXide)确实可以更好地显示命名空间信息。请参阅 eXide 演示 https://exist-db.org/exist/apps/eXide< /a>.
The eXist Sandbox results window unfortunately does not display namespace attributes (@xmlns). But if you save your query as an .xq file and run it via your browser, you'll see it actually is preserving the namespace information properly. By the way, the next generation version of the Sandbox, called eXide, does display namespace info somewhat better. See the demo of eXide at https://exist-db.org/exist/apps/eXide.
我在 OSB 11 上使用 Saxon 9.3 实现时遇到了类似的问题。奇怪的是,它适用于 Oxygen,但不适用于 OSB。
I have got a similar issue using a Saxon 9.3 implementation on OSB 11. Strangely it works on Oxygen but it doesn't on OSB.