使用 xquery 和 xdmp:node-replace 更新 xml 中的多个节点
我想更新 xml 数据库 (Marklogic) 中的 XML 文档。我有 xml 作为输入,并且想要替换目标 xml 中存在的每个节点。
如果一个节点不存在,如果添加它会很棒,但这可能是另一项任务。
我在数据库中的 XML:
<user>
<username>username</username>
<firstname>firstname</firstname>
<lastname>lastname</lastname>
<email>[email protected]</email>
<comment>comment</comment>
</user>
$user_xml 的值:
<user>
<firstname>new firstname</firstname>
<lastname>new lastname</lastname>
</user>
到目前为止我的功能:
declare function update-user (
$username as xs:string,
$user_xml as node()) as empty-sequence()
{
let $uri := user-uri($username)
return
for $node in $user_xml/user
return
xdmp:node-replace(fn:doc($uri)/user/fn:node-name($node), $node)
};
首先,我无法迭代 $user_xml/user
。如果我尝试迭代 $user_xml
我会得到异常
arg1 不是 node() 类型
,但也许它的方法是错误的?
有人可能有示例代码如何执行此操作吗?
I want to update an XML document in my xml database (Marklogic). I have xml as input and want to replace each node that exists in the target xml.
If a node does not exist it would be great if it gets added, but thats maybe another task.
My XML in the database:
<user>
<username>username</username>
<firstname>firstname</firstname>
<lastname>lastname</lastname>
<email>[email protected]</email>
<comment>comment</comment>
</user>
The value of $user_xml:
<user>
<firstname>new firstname</firstname>
<lastname>new lastname</lastname>
</user>
My function so far:
declare function update-user (
$username as xs:string,
$user_xml as node()) as empty-sequence()
{
let $uri := user-uri($username)
return
for $node in $user_xml/user
return
xdmp:node-replace(fn:doc($uri)/user/fn:node-name($node), $node)
};
First of all I cannot iterate over $user_xml/user
. If I try to iterate over $user_xml
I get the exception
arg1 is not of type node()
But maybe its the wrong approach anyway?
Does anybody maybe have sample code how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须自己回答:
但也许有人对
/user/*[fn:name() = fn:name($node)]
有更好的解决方案?I have to answer it myself:
but maybe someone has a better solution to
/user/*[fn:name() = fn:name($node)]
?