logo       

RE: replacing a node in in-memory XML: msg#00024

Subject: RE: replacing a node in in-memory XML
This is a multi-part message in MIME format.
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: talk-bounces-9zRdbLeNGsRBDgjK7y7TUQ@xxxxxxxxxxxxxxxx [mailto:talk-bounces-9zRdbLeNGsRBDgjK7y7TUQ@xxxxxxxxxxxxxxxx] On Behalf Of Robert Walpole
Sent: 13 November 2007 10:27
To: Wolfgang Meier
Cc: talk-KUd/JJajPrLQT0dZR+AlfA@xxxxxxxxxxxxxxxx
Subject: RE: [xquery-talk] 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.
 


From: Wolfgang Meier [mailto:wolfgangmm-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx]
Sent: Tue 06/11/2007 15:31
To: Robert Walpole
Cc: talk-KUd/JJajPrLQT0dZR+AlfA@xxxxxxxxxxxxxxxx
Subject: Re: [xquery-talk] 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


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
region.costa-ri...    user-groups.lin...    lang.prothon.us...    xfree86.cvs/200...    finance.aqbanki...    ietf.smtp/1993-...    web.turbogears....    jakarta.cactus....    yellowdog.gener...    php.gtk+.genera...    org.region.indo...    hurd.l4/2005-10...    culture.religio...    apple.fink.gene...    db.mysql.window...    bluez.devel/200...    security.pgp-ba...    video.blender.d...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe