Merging two XML docs and overwriting XML fields when found
I'm stumped at merging two XML documents together, I want the new second xml I'm merging with to overwrite the existing fields when found, and created when not;
<filemeta filetype="Video">
<heading>News Hea开发者_Python百科dlines</heading>
<shortblurb>The latest news roundup</shortblurb>
<description />
<files>
<file type="coverimage">headlines.png</file>
</files>
<Comments />
<AlbumTitle />
<TrackNumber />
<ArtistName />
<Year />
<Genre />
<TrackTitle />
<duration>00:02:22</duration>
<totalbitrate>1168 kb/s</totalbitrate>
<videocodec>h264</videocodec>
<pixelformat>yuv420p</pixelformat>
<resolution>640x360</resolution>
<audiocodec>aac</audiocodec>
<audiofrequency>44100 Hz</audiofrequency>
<channelmulplicity>stereo</channelmulplicity>
<audioformat>s16</audioformat>
<audiobitrate>111 kb/s</audiobitrate>
</filemeta>
and merge with this one;
<filemeta type="Video">
<duration>00:00:45</duration>
<totalbitrate>548 kb/s</totalbitrate>
<videocodec>h264</videocodec>
<pixelformat>yuv420p</pixelformat>
<resolution>720x576</resolution>
<audiocodec>aac</audiocodec>
<audiofrequency>48000 Hz</audiofrequency>
<channelmulplicity>stereo</channelmulplicity>
<audioformat>s16</audioformat>
<audiobitrate>65 kb/s</audiobitrate>
</filemeta>
I've tried working with various XSLT scripts and this however they only seem to append the second script to the end of the first one, thus invalidating my XML. Ideally I'd like todo this C#
Any help would be appreciated!
This is a possible (brute) XSLT 1.0 solution, just to give an idea.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="data2" select="document('test_i2.xml')/filemeta"/>
<xsl:template match="filemeta">
<xsl:copy>
<xsl:for-each select="*">
<xsl:variable name="element1" select="name(.)"/>
<xsl:choose>
<xsl:when test="count($data2/*[name()=$element1])!=0">
<xsl:copy-of select="$data2/*[name()=$element1]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Have look at this question, I think it will solve your problem.
精彩评论