Skip to content

Instantly share code, notes, and snippets.

@fgeorges
Created December 15, 2018 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgeorges/26288f7620d3340ea51b6989dd7bd046 to your computer and use it in GitHub Desktop.
Save fgeorges/26288f7620d3340ea51b6989dd7bd046 to your computer and use it in GitHub Desktop.
XQuery recursive descent copy
(:~
: Template for the recursive descent copy.
:
: This function does not actually modify anything as such. Modify it as needed,
: this version does by default, for each node, a recursive exact same copy.
:
: Note that in addition to typeswitch cases, you can also have regular if/else
: statements, for more complex conditions, but conditions that can be expressed
: as typeswitch cases will be more efficient.
:)
declare function local:modify($node as node())
{
typeswitch($node)
(: recursively copy documents and elements :)
case document-node() return
document {
$node/node() => local:modify()
}
case element() return
element { fn:node-name($node) } {
$node/(@*|node()) => local:modify()
}
(: copy attributes, text nodes, comments and PIs :)
case attribute() return
$node
case text() return
$node
case comment() return
$node
case processing-instruction() return
$node
(: in MarkLogic, you can have other node types, like binary() and object() :)
default return
fn:error((), 'Unexpected node type')
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment