Inside Out Outside In

XMLMerge UDF: Uses XSLT to merge root child nodes

This UDF will merge two XML objects and return a merged XML object.  It uses XSLT to merge the root children together.  download available through the boxNet widget.

<cffunction name="xmlMerge">
    <cfargument name="source1" type="xml">
    <cfargument name="source2" type="xml">

   
    <!--- strip off the root since we're doing inline --->
    <cfset var myXML = REFindNoCase("<#arguments.source2.xmlRoot.xmlName#>(.*)<\\/#arguments.source2.xmlRoot.xmlName#>",arguments.source2,1,true)>
    <cfset var childBody =  mid(arguments.source2,myXML.pos[2],myXML.len[2])>
    <cfset var myXSLT = "">
    <cfoutput>
        <cfsavecontent variable="myXSLT">
            <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
            <xsl:output method="xml"/>  
            <xsl:variable name="#arguments.source2.xmlRoot.xmlName#">
             #childBody#
            </xsl:variable> 
            <xsl:template match="/">    
            <#arguments.source1.xmlRoot.xmlName#>  
            <xsl:for-each select="/#arguments.source1.xmlRoot.xmlName#/#arguments.source2.xmlRoot.xmlChildren[1].xmlName#">                 
            <xsl:copy-of select="."/>      
            </xsl:for-each>
            <xsl:copy-of select="$#arguments.source2.xmlRoot.xmlName#" />
            <xsl:for-each select="/#arguments.source2.xmlRoot.xmlChildren[1].xmlName#">                 
            <xsl:copy-of select="."/>      
            </xsl:for-each>    
            </#arguments.source1.xmlRoot.xmlName#>  
            </xsl:template>
            </xsl:stylesheet>
        </cfsavecontent>
    </cfoutput>
    <cfreturn XMLParse(XMLTransform(arguments.source1,myXSLT))>
</cffunction>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)