
July 9th, 2003, 02:25 AM
|
 |
Contributing User
|
|
Join Date: Jul 2003
Posts: 206
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 6
|
|
|
Merge two xml files
Hi all,
I'm trying to merge two xml files but it doesn't work properly.
My aim is to get an xml file like:
Code:
<root>
<node>
<node>
<node text="In attribute 1.1.1 of file 1" text2="In attribute 1.1.1 of file 2">
</node>
</node>
<node>
<node text="In attribute 1.2.1 of file 1" text2="In attribute 1.2.1 of file 2">
</node>
</node>
</node>
<node>
<node>
<node>
</node>
</node>
</node>
<node>
<node>
<node>
</node>
</node>
</node>
</root>
What I would do is to add each text attribute of a node an other text attribute of the corresponding node in the other file.
I dont know have deep the tree is so that I can not give the path absolutly (like /path/path).
The two xml files:
file1:
Code:
<root>
<node>
<node>
<node text="In attribute 1.1.1 of file 1">
</node>
</node>
<node>
<node text="In attribute 1.2.1 of file 1">
</node>
</node>
</node>
<node>
<node>
<node>
</node>
</node>
</node>
<node>
<node>
<node>
</node>
</node>
</node>
</root>
and file2:
Code:
<root>
<node>
<node>
<node text="In attribute 1.1.1 of file 2">
</node>
</node>
<node>
<node text="In attribute 1.2.1 of file 2">
</node>
</node>
</node>
<node>
<node>
<node>
</node>
</node>
</node>
<node>
<node>
<node>
</node>
</node>
</node>
</root>
This is my xsl file:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="baum1" select="document('baum1.xml')" />
<xsl:variable name="baum2" select="document('baum2.xml')" />
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="root">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="node">
<xsl:variable name="thisindex" select="count(preceding::node())"/>
<xsl:if test="@text">
<node text ="{@text}"
text2="{document('baum2_1.xml')//descendant::node[$thisindex]/@text}"
index="{$thisindex}"/>
</xsl:if>
<xsl:if test="not(@text)">
<node>
<xsl:apply-templates />
</node>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Any ideas?
|