I would like to know how to modify an in-memory copy of original document stored in the DB. I am very happy with the update
extension, which allows me to search/replace through text nodes and change them permanently. However, this behavior is not always what I want. There are some special occasions when I need to export the document with minor changes done on the fly. It does not seem eXist supports copy
, which I would think about.
For permanent changes I use:
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 ')
};
(I iterate twice because it seems XPATH 2.0 does not allow to use look arounds in regexes and here matches are sometimes overlapping.)
How to do the same temporarily? I tried the interesting function from Datypic but it only returns particular nodes. I need to preserve the document order. Simply said, I need to go through a document tree, replace particular strings and return the document for latter usage as it is, without updating it in the DB.
UPDATE
Unfortunately, this:
declare function cust-utils:copy($input as item()*) as item()* {
for $node in $input
return $node
};
does absolutely the same as
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
};
… It seems it returns the document-node without real traversing.