使用 xquery 和 xdmp:node-replace 更新 xml 中的多个节点

发布于 2024-10-08 16:38:41 字数 1215 浏览 5 评论 0原文

我想更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2024-10-15 16:38:41

我必须自己回答:

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/*
    let $target := fn:doc($uri)/user/*[fn:name() = fn:name($node)]
    return
        if($target) then
            xdmp:node-replace($target, $node)
        else
            xdmp:node-insert-child(fn:doc($uri)/user, $node)
};

但也许有人对 /user/*[fn:name() = fn:name($node)] 有更好的解决方案?

I have to answer it myself:

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/*
    let $target := fn:doc($uri)/user/*[fn:name() = fn:name($node)]
    return
        if($target) then
            xdmp:node-replace($target, $node)
        else
            xdmp:node-insert-child(fn:doc($uri)/user, $node)
};

but maybe someone has a better solution to /user/*[fn:name() = fn:name($node)]?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文