From d7e614ce113602e3ee1eead5eefc340b5a6ed04c Mon Sep 17 00:00:00 2001 From: Peter Stadler Date: Wed, 24 May 2023 23:07:25 +0200 Subject: [PATCH] dynamically set the MerMEId version to `mei:application/@version` for new documents --- data/controller.xql | 5 ++++- modules/common.xqm | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/data/controller.xql b/data/controller.xql index 49504299..dbf15f65 100644 --- a/data/controller.xql +++ b/data/controller.xql @@ -190,7 +190,10 @@ else if($exist:path = '/create' and request:get-method() eq 'POST') then let $change-message := 'file created with MerMEId' let $template := if(doc-available($templatepath)) - then doc($templatepath) => common:set-mei-title-in-memory($title) => common:add-change-entry-to-revisionDesc-in-memory($username, $change-message) + then doc($templatepath) => + common:set-mei-title-in-memory($title) => + common:add-change-entry-to-revisionDesc-in-memory($username, $change-message) => + common:set-mermeid-version-info-in-memory() else () let $filename := request:get-parameter('filename', common:mermeid-id('file') || '.xml') let $overwrite := local:overwrite() diff --git a/modules/common.xqm b/modules/common.xqm index 4bce76b1..7960def9 100644 --- a/modules/common.xqm +++ b/modules/common.xqm @@ -302,3 +302,35 @@ declare function common:set-mei-title-in-memory($nodes as node()*, $new_title as declare function common:get-current-username() as xs:string? { sm:id()//sm:real/sm:username => data() }; + +(:~ + : Set/Update the MerMEId version within an MEI document in memory. + : NB, this will _not_ modify any existing document in the database! + : + : @param $node the input MEI document as node() or document-node() + : @param $new_title the new title + : @return the modified input node + :) +declare function common:set-mermeid-version-info-in-memory($nodes as node()*) as node()* { + for $node in $nodes + return + typeswitch($node) + case $elem as element(mei:application) return + if($elem/mei:name ='MerMEId') + then + element { node-name($elem) } { + attribute version {$config:version}, + $elem/@* except $elem/@version, + for $child in $elem/node() return common:set-mermeid-version-info-in-memory($child) + } + else + element { node-name($elem) } { + $elem/@*, for $child in $elem/node() return common:set-mermeid-version-info-in-memory($child) + } + case $elem as element() return + element { node-name($elem) } { + $elem/@*, for $child in $elem/node() return common:set-mermeid-version-info-in-memory($child) + } + case document-node() return document { common:set-mermeid-version-info-in-memory($node/node()) } + default return $node +};