|
[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message] replacing a node in in-memory XMLMichael Kay mike at saxonica.comTue Nov 13 13:00:40 PST 2007
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. Michael Kay http://www.saxonica.com/ _____ From: http://x-query.com/mailman/listinfo/talk [mailto:http://x-query.com/mailman/listinfo/talk] On Behalf Of Robert Walpole Sent: 13 November 2007 10:27 To: Wolfgang Meier Cc: http://x-query.com/mailman/listinfo/talk Subject: RE: replacing a node in in-memory XML 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: 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. Rob Walpole Devon Portal Developer Web: http://www.devonline.gov.uk Email: http://x-query.com/mailman/listinfo/talk _____ From: Wolfgang Meier [mailto:http://x-query.com/mailman/listinfo/talk] Sent: Tue 06/11/2007 15:31 To: Robert Walpole Cc: http://x-query.com/mailman/listinfo/talk Subject: Re: replacing a node in in-memory XML Hi Robert, > I am trying to figure out the best way to replace a node within an in-memory > XML fragment. I really like to use the typeswitch statement for things like this: declare function t:replace($node as node()) as node() { typeswitch ($node) case $elem as element(services) return <services> <service value="false">1</service> <service value="true">2</service> <service value="false">3</service> </services> case $elem as element() return element { node-name($elem) } { $elem/@*, for $child in $elem/node() return t:replace($child) } default return $node }; t:replace(doc("test.xml")/*) Wolfgang -------------- next part -------------- An HTML attachment was scrubbed... URL: http://x-query.com/pipermail/talk/attachments/20071113/0cb1c688/attachment.htm
|
Purchase Stylus Studio Online Today!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|






