Last active
October 19, 2018 16:25
-
-
Save ryanjdew/7250295 to your computer and use it in GitHub Desktop.
Example functional way of using my memory operations library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "1.0-ml"; | |
import module namespace mem = "http://maxdewpoint.blogspot.com/memory-operations" at "/memory-operations.xqy"; | |
import module namespace mem-fun = "http://maxdewpoint.blogspot.com/memory-operations/functional" at "/memory-operations-functional.xqy"; | |
declare variable $test-xml := <html> | |
<head> | |
<title>This is a title</title> | |
</head> | |
<!-- old comment --> | |
<body> | |
<div id="div1"> | |
<p class="p1"><!-- old comment -->This is a paragraph.</p> | |
<p class="p2">This is a paragraph.</p> | |
<p class="p3">This is a paragraph.</p> | |
<p class="p4">This is a paragraph.</p> | |
<p class="p5">This is a paragraph.</p> | |
</div> | |
<div id="div2"> | |
<p class="p1">This is a paragraph.</p> | |
<p class="p2">This is a paragraph.</p> | |
<p class="p3">This is a paragraph.<!-- old comment --></p> | |
<p class="p4">This is a paragraph.</p> | |
<p class="p5">This is a paragraph.</p> | |
</div> | |
</body> | |
</html>; | |
(: stateful way :) | |
let $new-xml := | |
let $transaction-id := mem:copy($test-xml) | |
let $_ := ( | |
mem:replace($transaction-id,$test-xml/head/title,element title {"This is so awesome!"}), | |
mem:insert-child($transaction-id,$test-xml/body/div/p,attribute data-info {"This is also awesome!"}) | |
) | |
return mem:execute($transaction-id) | |
(: functional way :) | |
let $functional-new-xml := | |
mem-fun:execute( | |
mem-fun:insert-child( | |
mem-fun:replace( | |
mem-fun:copy($test-xml), | |
$test-xml/head/title, | |
element title {"This is so awesome!"} | |
), | |
$test-xml/body/div/p, | |
attribute data-info {"This is also awesome!"} | |
) | |
) | |
return ($new-xml, $functional-new-xml) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment