declare function local:replaceServices($cg as node()) as
node()
{
typeswitch
($cg)
case $elem as
element(services) return (: if the element is a services element return the
following
:)
<services>
{
template:populateTemplateElement($elem,
true())
}
</services>
case $elem as
element() return (: if the element is anything else just return it as it is -
with child nodes intact :)
element {
node-name($elem) }
{
$elem/@*, for $child in $elem/node() return
local:replaceServices($child)
}
default return $cg
};
The $cg node is my in-memory node which contains the services
element I want to replace. The function template:populateTemplateElement
already existed so it was much easier for me to use this method than to go
down the XSLT route, which would've meant starting all over.
The template:populateTemplateElement function basically compares
the atomic values that exist in the record with all of the possible values
specified in the schema. It then returns elements containing all of the
possible atomic values from the schema with the addition of value attribute
containing an xs:boolean stating whether or not the group has this
value.
The true() parameter passed to the function is simply to tell the
function whether to return "Yes"/"No" in the value attrib or "true"/"false".
In this case I want "Yes"/"No".
Thanks again for all your suggestions and some very
interesting comments.