<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Re: [xquery-talk] replacing a node in in-memory XML</TITLE>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.6000.16544" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=191225712-13112007><FONT face=Arial 
color=#0000ff size=2>One final comment: note that this method will drop "unused" 
namespace declarations from the source document. Not a problem for everyone, 
positively welcome in some cases - but unfortunate if your document contains 
namespace-sensitive content such as xsi:type attributes. </FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=191225712-13112007><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=191225712-13112007><FONT face=Arial 
color=#0000ff size=2>Michael Kay</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=191225712-13112007><FONT face=Arial 
color=#0000ff size=2>http://www.saxonica.com/</FONT></SPAN></DIV>
<BLOCKQUOTE dir=ltr 
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
  <DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
  <HR tabIndex=-1>
  <FONT face=Tahoma size=2><B>From:</B> talk-bounces@x-query.com 
  [mailto:talk-bounces@x-query.com] <B>On Behalf Of </B>Robert 
  Walpole<BR><B>Sent:</B> 13 November 2007 10:27<BR><B>To:</B> Wolfgang 
  Meier<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></DIV>
  <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></BLOCKQUOTE></BODY></HTML>