<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> typeswitch
($cg)<BR> case $elem as
element(services) return (: if the element is a services element return the
following
:)<BR>
<services><BR>
{<BR>
template:populateTemplateElement($elem,
true())<BR>
}<BR>
</services><BR> case $elem as
element() return (: if the element is anything else just return it as it is -
with child nodes intact :)
<BR> element {
node-name($elem) }
{<BR>
$elem/@*, for $child in $elem/node() return
local:replaceServices($child)<BR>
}<BR> default return $cg<BR>};</DIV>
<DIV dir=ltr> </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> </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> </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> </DIV>
<DIV dir=ltr>Thanks again for all your suggestions and some very
interesting comments.</DIV>
<DIV dir=ltr> </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>> I am trying to figure out the best way to
replace a node within an in-memory<BR>> 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> typeswitch
($node)<BR> case $elem as
element(services)
return<BR>
<services><BR>
<service
value="false">1</service><BR>
<service
value="true">2</service><BR>
<service
value="false">3</service><BR>
</services><BR> case $elem as
element() return<BR>
element { node-name($elem) }
{<BR>
$elem/@*, for $child in $elem/node() return
t:replace($child)<BR>
}<BR> default return
$node<BR>};<BR><BR>t:replace(doc("test.xml")/*)<BR><BR>Wolfgang<BR></FONT></P></DIV>
</BODY>
</HTML>