Re: [xsl] Need to override previously copied attributes etc.

Subject: Re: [xsl] Need to override previously copied attributes etc.
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Thu, 28 Jul 2005 17:50:21 +0200
Hi,
Tempore 02:25:13, die 07/28/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Taco Fleur <taco@xxxxxxxxxxxxx>:

I have a stylesheet which basically should do the following:
- Copy the whole XML file passed to it
- Apply a template if the element is "field"
	- Copy everything over from the XML file passed
	- Then start copying everything from the XML in a variable
($fieldCollection) where the attribute "identity" matches
		- However if the element from $fieldCollection contains
an attribute called "reference" is present it needs to copy everything
from the element in $fieldCollection that matches that "identity"
		- Then it just needs to copy over that element from
$fieldCollection and overwrite any attributes previously copied
		- And finally it needs to copy over the element from the
XML passed in and overwrite anything previously there

In Saxon, copied attributes override previously copied attributes. The following stylesheet gives the desired result:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output omit-xml-declaration="yes" indent="yes" media-type="string" />

<xsl:variable name="fieldCollection" select="document('fieldcollection.xml')/root"/>

<xsl:template match="*">
	<xsl:copy>
		<xsl:copy-of select="@*"/>
		<xsl:apply-templates />
	</xsl:copy>
</xsl:template>

<xsl:template match="field">
<xsl:variable name="match" select="$fieldCollection/field[current()/@identity = @identity ]"/>
<xsl:copy>
	<xsl:copy-of select="@*" />
	<xsl:apply-templates select="$match" mode="collect_attributes"/>
	<xsl:copy-of select="*"/>
	<xsl:apply-templates select="$match" mode="collect_elements"/>
</xsl:copy>
</xsl:template>

<xsl:template match="field" mode="collect_attributes">
	<xsl:param name="exclude">-identity-reference-</xsl:param>
	<xsl:copy-of select="@*[not(contains($exclude,concat('-',local-name(),'-')))]"/>
	<xsl:apply-templates select="../field[@identity=current()/@reference]" mode="collect_attributes">
		<xsl:with-param name="exclude">-identity-reference-name-</xsl:with-param>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="field" mode="collect_elements">
	<xsl:copy-of select="*"/>
	<xsl:apply-templates select="../field[@identity=current()/@reference]" mode="collect_elements"/>
</xsl:template>

</xsl:stylesheet>



regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Vincit omnia simplicitas
Keep it simple

Current Thread