I have two large XML files, where some text in the first one is wrong and needs to be replaced with text from another xml file.
xml 1
<phrase name="1111"><![CDATA[aaaa]]></phrase>
<phrase name="2222"><![CDATA[bbbb]]></phrase>
<phrase name="3333"><![CDATA[cccc]]></phrase>
...
and
xml 2
<phrase name="1111"><![CDATA[dddd]]></phrase>
<phrase name="2222"><![CDATA[eeee]]></phrase>
<phrase name="4444"><![CDATA[ffff]]></phrase>
...
Now the text in elements with the same name should be overwriten with the text from xml 2
output
<phrase name="1111"><![CDATA[dddd]]></phrase>
<phrase name="2222"><![CDATA[eeee]]></phrase>
<phrase name="3333"><![CDATA[cccc]]></phrase>
...
Any ideas on how to do this as easy as possible? I already tried using XSLT, but im new to it and it didnt work, the way i need it. Is doesn't need to be done with XSLT, if there is any programm that can handle this.
thanks for your help
The following transformation
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output cdata-section-elements="phrase" />
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="//phrase">
<xsl:variable name="name"><xsl:value-of select="@name" /></xsl:variable>
<xsl:copy><xsl:choose>
<xsl:when test="document('second.xml')//phrase[@name=$name]">
<xsl:apply-templates select="@*" />
<xsl:value-of select="document('second.xml')//phrase[@name=$name]" />
</xsl:when>
<xsl:otherwise><xsl:apply-templates select="@*|node()" /></xsl:otherwise>
</xsl:choose></xsl:copy>
</xsl:template>
</xsl:transform>
when applied to an input file first.xml the form of
<doc>
<phrase name="1111"><![CDATA[aaaa]]></phrase>
<phrase name="2222"><![CDATA[bbbb]]></phrase>
<phrase name="3333"><![CDATA[cccc]]></phrase>
</doc>
where a second input file second.xml the form of
<doc>
<phrase name="1111"><![CDATA[dddd]]></phrase>
<phrase name="2222"><![CDATA[eeee]]></phrase>
<phrase name="4444"><![CDATA[ffff]]></phrase>
</doc>
is present produces the wanted result of
<doc>
<phrase name="1111"><![CDATA[dddd]]></phrase>
<phrase name="2222"><![CDATA[eeee]]></phrase>
<phrase name="3333"><![CDATA[cccc]]></phrase>
</doc>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With