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

Subject: RE: [xsl] Need to override previously copied attributes etc.
From: "Taco Fleur \(@DataBroker\)" <taco@xxxxxxxxxxxxx>
Date: Fri, 29 Jul 2005 09:24:08 +1000
Hi thanks for the reply.
When I run that as is, I do not get the desired result, also I don't know
what to do with 
-identity-reference- and -identity-reference-name- if anything?

It basically just copied everything over from the passed in file, but it did
not look to see if an element should get its attributes/nodes from a
referenced element.

Groetjes.

-----Original Message-----
From: Joris Gillis [mailto:roac@xxxxxxxxxx] 
Sent: Friday, 29 July 2005 1:50 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Need to override previously copied attributes etc.

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