Vote count:
0
Background
I'd like to update an XML file by merging new content into an existing file. Everything in the existing file (A) should be left unchanged except for the content to be merged (file B), in this case, 'description' nodes.
My question is similar to this one. However, a limitation I've encountered with the answer suggested is that node order is not necessarily preserved. I would like original node order to be preserved if possible. My current XSL only does the following:
- matches the description nodes to be merged, then copies them over.
- then copies everything else (everything that's not a description).
My XSL and XML are below. Do note that I'm limited to XSLT 1.0
XSL
<xsl:variable name="desc" select="document('doc.out.xml')" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//*[name=$desc//node/@name]">
<xsl:variable name="name" select="current()/name" />
<xsl:copy>
<xsl:apply-templates select="$desc//node[@name=$name]/description" />
<xsl:apply-templates select="@*|node()[not(self::description)]" />
</xsl:copy>
</xsl:template>
XML A (to be updated)
<renderer>
<name>key</name>
<description>
The key element is an Object Element used to create and
identify rows.
</description>
<fields>
<field>
<name>elements</name>
<description>
A container for all 'element' nodes.
</description>
<factory>
<localRenderer>
<name>element</name>
<description>
A primitive object and arrays thereof.
</description>
<fields>
<field>
<name>name</name>
<description>
Alias by which the element may be referenced.
</description>
</field>
<field>
<name>type</name>
<description>
Type of the data.
</description>
</field>
</fields>
</localRenderer>
</factory>
</field>
</fields>
</renderer>
XML B (contains content to be merged into A)
<root>
<node name="key">
<description>The key element is an Object Element used to create and
identify rows.Note that object declaration via XML must occur within
a definitions element. See for more information.</description>
<subnodes>
<node name="elements">
<description>
Hi. This is a test.
</description>
<subnodes>
<node name="element">
<description>A primitive object and arrays thereof. TEST</description>
<subnodes>
<node name="name">
<description>Alias by which the element may be referenced.
</description>
</node>
<node name="type">
<description>Type of the data; a primitive object of array
thereof. </description>
</node>
</subnodes>
</node>
</subnodes>
</node>
</subnodes>
</node>
</root>
asked 1 min ago
XSLT: Update nodes via merge without changing node order?
Aucun commentaire:
Enregistrer un commentaire