MMM Posted June 4, 2012 Share Posted June 4, 2012 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. Link to comment Share on other sites More sharing options...
boen_robot Posted June 4, 2012 Share Posted June 4, 2012 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> Link to comment Share on other sites More sharing options...
MMM Posted June 4, 2012 Author Share Posted June 4, 2012 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> Link to comment Share on other sites More sharing options...
boen_robot Posted June 4, 2012 Share Posted June 4, 2012 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> Link to comment Share on other sites More sharing options...
MMM Posted June 5, 2012 Author Share Posted June 5, 2012 Big thanks boen_robot. Works great like this: <xsl:apply-templates select="@* | node() | document('my2ndFile.xml')/CAEXFile/SystemUnitClassLib" /> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now