<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>

<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7638.1">
<TITLE>Re: [xquery-talk] replacing a node in in-memory XML</TITLE>
</HEAD>
<BODY>
<DIV id=idOWAReplyText85500 dir=ltr>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2>Just to complete the thread, 
I have now got this working exactly as I wanted using a modification of the code 
provided by Wolfgang. Here is the complete function:</FONT></DIV><FONT 
face=Arial color=#000000 size=2>
<DIV dir=ltr><BR>declare function local:replaceServices($cg as node()) as 
node()<BR>{<BR>&nbsp;&nbsp;&nbsp; typeswitch 
($cg)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case $elem as 
element(services) return (: if the element is a services element return the 
following 
:)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;services&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
template:populateTemplateElement($elem, 
true())<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/services&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case $elem as 
element() return (: if the element is anything else just return it as it is - 
with child nodes intact :) 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; element { 
node-name($elem) } 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
$elem/@*, for $child in $elem/node() return 
local:replaceServices($child)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default return $cg<BR>};</DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>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.</DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>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.</DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>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".</DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>Thanks again for all your suggestions and some very 
interesting&nbsp;comments.</DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>Rob Walpole<BR>Devon Portal Developer<BR>Web: <A 
href="http://www.devonline.gov.uk">http://www.devonline.gov.uk</A><BR>Email: <A 
href="mailto:robert.walpole@devon.gov.uk">robert.walpole@devon.gov.uk</A></FONT></DIV></FONT></DIV></DIV>
<DIV dir=ltr><BR>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> Wolfgang Meier 
[mailto:wolfgangmm@gmail.com]<BR><B>Sent:</B> Tue 06/11/2007 15:31<BR><B>To:</B> 
Robert Walpole<BR><B>Cc:</B> talk@xquery.com<BR><B>Subject:</B> Re: 
[xquery-talk] replacing a node in in-memory XML<BR></FONT><BR></DIV>
<DIV>
<P><FONT size=2>Hi Robert,<BR><BR>&gt; I am trying to figure out the best way to 
replace a node within an in-memory<BR>&gt; XML fragment.<BR><BR>I really like to 
use the typeswitch statement for things like this:<BR><BR>declare function 
t:replace($node as node()) as node() {<BR>&nbsp;&nbsp;&nbsp; typeswitch 
($node)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case $elem as 
element(services) 
return<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;services&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;service 
value="false"&gt;1&lt;/service&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;service 
value="true"&gt;2&lt;/service&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;service 
value="false"&gt;3&lt;/service&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&lt;/services&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case $elem as 
element() return<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
element { node-name($elem) } 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
$elem/@*, for $child in $elem/node() return 
t:replace($child)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default return 
$node<BR>};<BR><BR>t:replace(doc("test.xml")/*)<BR><BR>Wolfgang<BR></FONT></P></DIV>

</BODY>
</HTML>