我想知道如何修改存储在DB中的原始文档的内存中副本.我对update扩展非常满意,它允许我通过文本节点搜索/替换并永久地更改它们.但是,这种行为并不总是我想要的.在某些特殊情况下,我需要导出文档,并在运行中进行微小的更改.它似乎不是eXist支持copy,我会考虑.
对于永久性更改,我使用:
declare function cust-utils:replace-spaces-hard($document as xs:string) as empty() {
let $doc := doc($document)/tei:TEI
let $match := '(^|\s| )([szkvaiouSZKVAIOU])[\s]'
for $i in (1 to 2)
return
for $text in $doc//text()
return
update value $text[matches(., $match)] with replace($text, $match, '$1$2 ')
};
Run Code Online (Sandbox Code Playgroud)
(我迭代两次,因为看起来XPATH 2.0不允许在正则表达式中使用环绕声,这里的匹配有时会重叠.)
如何临时做同样的事?我尝试了Datypic中有趣的函数,但它只返回特定的节点.我需要保留文档顺序.简单地说,我需要浏览一个文档树,替换特定的字符串并按原样返回文档,而不是在DB中更新它.
UPDATE
不幸的是,这个:
declare function cust-utils:copy($input as item()*) as item()* {
for $node in $input
return $node
};
Run Code Online (Sandbox Code Playgroud)
绝对一样
declare function cust-utils:copy($input as item()*) as item()* {
for $node in $input
return
typeswitch($node)
case element()
return
element { name($node) } {
for $att in $node/@*
return
attribute { name($att) } { $att }
,
(: output all the sub-elements of this element recursively :)
for $child in $node
return cust-utils:copy($child/node())
}
default return $node
};
Run Code Online (Sandbox Code Playgroud)
...它似乎在没有实际遍历的情况下返回文档节点.
eXist的XQuery Update扩展将所有更新写入数据库,不支持内存中操作.这与e3ist不支持的W3C XQuery Update Facility 1.0+形成对比.因此,在eXist中,必须使用纯XQuery执行内存中更新,即,没有正式Update工具的附加语法和功能.
对于使用eXist的内存更新,传统的路径是执行"身份转换",通常使用递归typeswitch操作; 请参阅https://en.wikipedia.org/wiki/Identity_transform#Using_XQuery.显示文本节点转换同时保留文档顺序的简单示例是:
xquery version "3.0";
declare function local:transform($nodes as node()*) {
for $node in $nodes
return
typeswitch ($node)
case document-node() return
local:transform($node/node())
case element() return
element {node-name($node)} {
$node/@*,
local:transform($node/node())
}
case text() return
replace($node, '[a-z]+', upper-case($node))
(: drop comment & processing-instruction nodes :)
default return
()
};
let $node :=
document {
element root {
comment { "sample document" },
element x {
text { "hello" },
element y {
text { "there" }
},
text { "friend" }
}
}
}
return
<results>
<before>{$node}</before>
<after>{local:transform($node)}</after>
</results>
Run Code Online (Sandbox Code Playgroud)
结果:
<result>
<before>
<root>
<!-- sample document -->
<x>hello <y>there</y> friend</x>
</root>
</before>
<after>
<root>
<x>HELLO <y>THERE</y> FRIEND</x>
</root>
</after>
</result>
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用内存更新模块,例如Ryan J. Dew的"XQuery XML Memory Operations"模块,位于https://github.com/ryanjdew/XQuery-XML-Memory-Operations.如果您克隆存储库(或下载存储库的.zip文件并将其解压缩)并将文件夹上传到eXist的/db集合,则以下代码将起作用(改编自这个旧的存在 - 开放帖子:http://markmail.org/message/ pfvu5omj3ctfzrft):
xquery version "3.0";
import module namespace mem="http://maxdewpoint.blogspot.com/memory-operations"
at "/db/XQuery-XML-Memory-Operations-master/memory-operations-pure-xquery.xqy";
let $node := <x>hello</x>
let $copy := mem:copy($node)
let $rename := mem:rename($copy, $node, fn:QName("foo", "y"))
let $replace-value := mem:replace-value($rename, $node, "world")
return
mem:execute($replace-value)
Run Code Online (Sandbox Code Playgroud)
结果:
<y xmlns="foo">world</y>
Run Code Online (Sandbox Code Playgroud)