I am trying to merge 2 xml files in to one. They simply need to one another. Any help would be appreciated.

File 1:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog>
	<data>
		<title>Title1</title>
		<description>Description1</description>
	</data>
	<data>
		<title>Title2</title>
		<description>Description2</description>
	</data>
</catalog>


File 2:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog>
	<data>
		<title>Title3</title>
		<description>Description1</description>
	</data>
	<data>
		<title>Title4</title>
		<description>Description2</description>
	</data>
</catalog>


Expected result:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<catalog>
	<data>
		<title>Title1</title>
		<description>Description1</description>
	</data>
	<data>
		<title>Title2</title>
		<description>Description2</description>
	</data>
	<data>
		<title>Title3</title>
		<description>Description1</description>
	</data>
	<data>
		<title>Title4</title>
		<description>Description2</description>
	</data>
</catalog>

Dani AI

Generated

This thread seeks a simple merge of two XML files that share the same root (a single catalog containing multiple repeated item nodes). showed the desired final structure; pointed to an XSLT route and confirmed the link was useful. Below are two practical, well-tested approaches (XSLT and a tiny Python script) plus concise cautions so the merged output is valid and predictable.

A minimal XSLT (run with file1 as the input) that appends all child nodes from file2:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/>
  <xsl:template match="/catalog">
    <catalog>
      <xsl:copy-of select="data"/>
      <xsl:copy-of select="document('file2.xml')/catalog/data"/>
    </catalog>
  </xsl:template>
</xsl:stylesheet>

A straightforward Python solution using ElementTree (good for small-to-medium files):

import xml.etree.ElementTree as ET

a = ET.parse('file1.xml')
b = ET.parse('file2.xml')

root = a.getroot()
for child in b.getroot():
    root.append(child)

a.write('merged.xml', encoding='ISO-8859-1', xml_declaration=True)

Quick practical notes and pitfalls: both files must result in a single root element — simply concatenating files will produce an invalid XML with two declarations; prefer a merge operation that builds one root. Confirm both files use the same encoding (ISO-8859-1 in this case) or normalize to Unicode in code. Watch for namespaces, duplicate IDs/keys, and mixed schemas; those may require element-level reconciliation rather than blind appends. For very large XML, use streaming parsing (iterparse/SAX) to avoid memory spikes. Finally, validate the merged output with a parser or xmllint to ensure well-formedness.

Relevant references: XSLT basics at the W3C XSLT 1.0 spec and Python ElementTree docs (ElementTree).

Recommended Answers

All 2 Replies

A quick google found this.

Link

@MikeyIsMe that was a very helpful link

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.