Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xQuery更改内存中的XML值_Xml_Xquery_Marklogic - Fatal编程技术网

使用xQuery更改内存中的XML值

使用xQuery更改内存中的XML值,xml,xquery,marklogic,Xml,Xquery,Marklogic,我试图更改从web表单加载到内存中的一个非常大的XML文件中几个节点的值 文件的获取方式如下: let $file := xdmp:get-request-field("xml_to_upload") 因此,正如您所看到的,该文件位于内存中 现在,我需要更改数千个节点的值,但到目前为止,我还无法以最佳方式进行更改 有什么想法吗 到目前为止我尝试过的一些事情: let $auxVar := if($fileStructureIsValid) then (

我试图更改从web表单加载到内存中的一个非常大的XML文件中几个节点的值

文件的获取方式如下:

let $file := xdmp:get-request-field("xml_to_upload")
因此,正如您所看到的,该文件位于内存中

现在,我需要更改数千个节点的值,但到目前为止,我还无法以最佳方式进行更改

有什么想法吗

到目前为止我尝试过的一些事情:

let $auxVar :=
        if($fileStructureIsValid) then
        (
            for $currentNode in xdmp:unquote($file)//ID

            let $log := xdmp:log( fn:concat( "newNodeValue", ": ", mem:replace( $currentNode, element ID{ fn:concat( $subject, "-", fn:data( $currentNode ) ) } ) ) )

                return fn:concat( $subject, "-", fn:data( $currentNode ) )
        )
        else
        (

        )

mem库是自定义下载的。

如果可能,将文档插入数据库,并在单独的事务中使用
xdmp:node replace
更新节点

xquery version "1.0-ml";
...
xdmp:document-insert('file.xml', $file) ;

xquery version "1.0-ml";

for $currentNode in doc('file.xml')//ID
return xdmp:node-replace($currentNode,
  element ID{ concat($subject, "-", $currentNode) });
或者,如果您必须更新内存中的文档,则最好只遍历一次树(使您的更新都在该操作中),而不是执行多个
mem:replace
操作(每个操作可能都会重新遍历树)

更新:

正如Erik在评论中所建议的那样,您还可以在XSLT中编写
local:updateid
的逻辑(使用
xdmp:XSLT eval
xdmp:XSLT invoke
执行),它们的性能应该大致相当。事实上,MarkLogic在这个主题上有一篇写得非常好的博客:


为了完整性,第三种选择是对内存中的文档应用XSLT转换。
declare function local:update-ids(
  $n as item(),
  $subject as xs:string
) as item()
{
  typeswitch ($n)
    case element(ID) return 
      element ID { concat($subject, "-", $n) }
    case element() return
      element { node-name($n) } {
        @*, $n/node()/local:update-ids(., $subject) }
    default return $n
};

let $xml := xdmp:unquote($file)
let $xml-with-updated-ids := local:update-ids($xml, $subject)
...