简体   繁体   English

如何在 MarkLogic 中重命名文档?

[英]How to rename a document in MarkLogic?

I have simple task to do but unable to find the exact solutions for this.I have saved a file as abc.xml in MarkLogic.How can i rename the file as some example.xml using XQuery?我有一个简单的任务要做,但无法找到确切的解决方案。我在 MarkLogic 中将文件另存为 abc.xml。如何使用 XQuery 将该文件重命名为某个 example.xml?

Code which I tried:我试过的代码:

xquery version "1.0-ml";
xdmp:document-rename ("/aaa.xml","/final.xml");

This is showing an error.这是显示错误。

There is no way, that I know of, to change the document URI of an existing document. 我知道,改变现有文档的文档URI是没有办法的。 The only way I can think of is to create a new document with the same content and the new URI, and delete the existing one, in the same transaction. 我能想到的唯一方法是在同一个事务中创建一个具有相同内容和新URI的新文档,并删除现有文档。

Where it gets tricky is to make sure to preserve the ownership, the permissions, all the properties, the property document, make sure that the old URI is not used anywhere to link to the existing document, etc. 它变得棘手的是确保保留所有权,权限,所有属性,属性文档,确保旧URI不用于任何地方链接到现有文档等。

But usually, the document URI is never really used. 但通常,文档URI从未真正使用过。 You should first considering whether you really need to rename the document, and why. 您应首先考虑是否确实需要重命名文档,以及原因。

(Note that saying "this is showing an error" is rarely useful on SO or on mailing lists, if you do not show what the error is.) (注意,如果您没有显示错误是什么,说“这显示错误”在SO或邮件列表上很少有用。)

Florent is correct, a true 'rename' is not possible, or perhaps not even meaningful. 弗洛伦特是正确的,真正的“重命名”是不可能的,或者甚至可能没有意义。 ( analogy - rename a file from one disk to another ) (比喻 - 将文件从一个磁盘重命名为另一个磁盘)

"Move" however is meaningful (copy then delete in a transaction). 然而,“移动”是有意义的(复制然后在事务中删除)。 Defining "Move" is use case dependent - ie what metatdata also needs to 'move' ? 定义“移动”是依赖于用例 - 即metatdata还需要'移动'? permissions? 权限? collections ? 收藏? document properties ? 文件属性? inherited permissions ? 继承权限?

xmlsh ( http://www.xmlsh.org ) implements a 'rename' ( http://www.xmlsh.org/MarkLogicRename ) command for the marklogic extension which is really a 'move', with the implemenation borrowed from postings on markmail ( http://markmail.org/ ) xmlsh( http://www.xmlsh.org )为marklogic扩展实现了一个'rename'( http://www.xmlsh.org/MarkLogicRename )命令,这个命令实际上是一个'移动',其实现是从帖子借来的markmail( http://markmail.org/

The implementation is the following XQuery - it doesnt do everything you might want and it might do more then you want. 实现是以下XQuery - 它不会做你想要的一切,它可能会做你想要的更多。 YMMV 因人而异

https://github.com/DALDEI/xmlsh/blob/master/extensions/marklogic/src/org/xmlsh/marklogic/resources/rename.xquery https://github.com/DALDEI/xmlsh/blob/master/extensions/marklogic/src/org/xmlsh/marklogic/resources/rename.xquery

( it was also written long ago - it is likely to benefit from improvement ) (它也是很久以前写的 - 它可能会从改进中受益)

I have working example this works for me. 我有工作的例子,这对我有用。

xquery version "1.0-ml";

 declare function local:document-rename( $old-uri as xs:string, $new-uri as xs:string) as empty-sequence() { xdmp:document-delete($old-uri), let $permissions := xdmp:document-get-permissions($old-uri) let $collections := xdmp:document-get-collections($old-uri) return xdmp:document-insert( $new-uri, doc($old-uri), if ($permissions) then $permissions else xdmp:default-permissions(), if ($collections) then $collections else xdmp:default-collections(), xdmp:document-get-quality($old-uri) ) , let $prop-ns := namespace-uri(<prop:properties/>) let $properties := xdmp:document-properties($old-uri)/node() [ namespace-uri(.) ne $prop-ns ] return xdmp:document-set-properties($new-uri, $properties) }; 

(: function call :) local:document-rename ("/opt/backup/x.xml","y.xml");

MarkLogic has a tutorial up addressing file renaming (moving): MarkLogic 有一个解决文件重命名(移动)的教程:

https://developer.marklogic.com/recipe/move-a-document/ https://developer.marklogic.com/recipe/move-a-document/

Importantly, it uses the function xdmp:lock-for-update() to prevent modifications to the source file while it is being copied to the target location.重要的是,它使用函数 xdmp:lock-for-update() 来防止在将源文件复制到目标位置时对其进行修改。

Also, if you are doing a batch renaming you'll want to make sure that each file URI you rename corresponds to a document in the database or you'll get runtime errors.此外,如果您要进行批量重命名,您需要确保重命名的每个文件 URI 对应于数据库中的一个文档,否则会出现运行时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM