Jump to content
Sign in to follow this  
MMM

(Easy?) XML Merge

Recommended Posts

Hi, I'm new to XSLT and I got some problems with it. I want to merge 2 XML files. I want to copy everything from a main file and a specific element with its childs from a second file into the main root Element. Example: Main File:

<?xml version="1.0" encoding="utf-8"?><CAEXFile SchemaVersion="2.15" ...>	<InstanceHierarchy Name="Example">		...A lot of elements...	</InstanceHierarchy></CAEXFile>

2nd File:

<CAEXFile ...>		<SystemUnitClassLib Name="Function">	...A lot of elements...	</SystemUnitClassLib></CAEXFile>

Final File:

<?xml version="1.0" encoding="utf-8"?><CAEXFile SchemaVersion="2.15" ...>	<InstanceHierarchy Name="Example">		...A lot of elements...	</InstanceHierarchy>	<SystemUnitClassLib Name="Function">	...A lot of elements...	</SystemUnitClassLib></CAEXFile>

My try looks like this:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="CAEXFile"><xsl:copy>   <xsl:apply-templates select="@* | node()" /></xsl:copy></xsl:template> <xsl:template match="node() | @*"><xsl:copy-of select="." /></xsl:template> </xsl:stylesheet>

It copies the file correct, but how do I get the information out of the 2nd file? I tried something like this:

   <xsl:apply-templates	match="document('my2ndFile.xml')/SystemUnitClassLib/node()" />

but without success. I found some Topics which had the same issue, but it couldn't help me. I'd be very thankful for any help.

Share this post


Link to post
Share on other sites

Your XPath on the document() line is wrong... it should start with the file's root element, i.e. "CAEXFile". Try it like:

<xsl:template match="CAEXFile"><xsl:copy>   <xsl:apply-templates select="@* | node()" />   <xsl:apply-templates match="document('my2ndFile.xml')/CAEXFile/SystemUnitClassLib/node()" /></xsl:copy></xsl:template>

Share this post


Link to post
Share on other sites

Ahh ok, one mistake. But with a XSLT file like this:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="CAEXFile"><xsl:copy>   <xsl:apply-templates select="@* | node()" />   <xsl:apply-templates match="document('my2ndFile.xml')/CAEXFile/SystemUnitClassLib/node()" /></xsl:copy></xsl:template> <xsl:template match="node() | @*"><xsl:copy-of select="." /></xsl:template> </xsl:stylesheet>

I receive the content of the main file doubled:

<?xml version="1.0" encoding="utf-8"?><CAEXFile SchemaVersion="2.15" ...>		<InstanceHierarchy Name="Example">				...A lot of elements...		</InstanceHierarchy>		<InstanceHierarchy Name="Example">				...A lot of elements...		</InstanceHierarchy></CAEXFile>

Share this post


Link to post
Share on other sites

Try it with a single apply-templates:

<xsl:template match="CAEXFile"><xsl:copy>   <xsl:apply-templates select="@* | node() | document('my2ndFile.xml')/CAEXFile/SystemUnitClassLib/node()" /></xsl:copy></xsl:template>

Share this post


Link to post
Share on other sites

Big thanks boen_robot. Works great like this:

<xsl:apply-templates select="@* | node() | document('my2ndFile.xml')/CAEXFile/SystemUnitClassLib" />

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...