Using some information I found here "Use XSLT to Merge XML Documents" [http://www.fawcette.com/archives/premier/mgznarch/xml/2001/06jun01/rj0103/rj0103.asp], I got this to work in a more natively XMLish way without relying on brute force deep copying or possibly depreciated hidden functions.  

I'm including test code that show the two ways I discovered to do this.  The first uses some hidden functionality found here [http://www.spike.org.uk/blog/index.cfm] posted on the June 4th entry, thanks to Massimo Foti.  This one does have the advantage of working on two xml documents in memory.

The second method relies on straight forward xml and xls, but it does require that one of the xml documents be a file the other can be in memory.  It would be interesting if there was some way around this.

If anybody plays with this and develops improvements, I would love to hear about them.

Thank you all for your help.

--------------
Ian Skinner
Web Programmer
BloodSource
www.BloodSource.org
Sacramento, CA

"C code. C code run. Run code run. Please!"
- Cynthia Dunning

Here is my test code.

<cfxml variable="doc1">
<root>
<member id="10">
<name>Ian</name>
<sex>male</sex>
</member>
</root>
</cfxml>

<cfxml variable="doc2">
<root>
<member id="1">
<name>Joe</name>
<sex>male</sex>
</member>
<member id="2">
<name>John</name>
<sex>male</sex>
</member>
<member id="3">
<name>Sue</name>
<sex>female</sex>
</member>
</root>
</cfxml>

<cffile action="" file="#expandPath("/")#\doc2.xml" output="#ToString(doc2)#" nameconflict="overwrite">

<!--- METHOD ONE using a depreciated and undocumented method --->
<cfset newNode = doc1.root.member.cloneNode(true)>
<cfset doc2.changeNodeOwner(newNode)>
<cfset doc2.root.appendChild(newNode)>

<cfdump var="#doc2#" label="Merged by hidden functions" expand="no">

<!--- METHOD TWO using XSLT --->
<!--- create an xsl docutment--->
<cfoutput>
<cfsavecontent variable="transformer">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<!--load the merge file -->
<xsl:variable name="emps" select="document('#expandPath("/")#\doc2.xml')"/>
<!--- this line references an XML file to be combined with the main file,
I wonder if there is a way to use an XML document in memory here. --->

<!-- combine the files -->
<xsl:template match="/">
<root>
<!-- select all the child nodes of the root tag in the main file -->
<xsl:for-each select="root/child::*">
<!-- copy the member tag -->
<xsl:copy-of select="."/>
</xsl:for-each>

<!-- and all the child nodes of the root tag in the merge file -->
<xsl:for-each select="$emps/root/child::*">
<!-- copy the member tag -->
<xsl:copy-of select="."/>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
</cfsavecontent>
</cfoutput>

<!--- create a combined xml document --->
<cfset doc2 = XMLparse(XMLtransform(doc1,transformer))>

<cfdump var="#doc2#" label="Merged by XSLT" expand="no">

Confidentiality Notice:  This message including any
attachments is for the sole use of the intended
recipient(s) and may contain confidential and privileged
information. Any unauthorized review, use, disclosure or
distribution is prohibited. If you are not the
intended recipient, please contact the sender and
delete any copies of this message.
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to