[xsl] Need to override previously copied attributes etc.

Subject: [xsl] Need to override previously copied attributes etc.
From: "Taco Fleur" <taco@xxxxxxxxxxxxx>
Date: Thu, 28 Jul 2005 10:25:13 +1000
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


<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Author: Taco Fleur (tacofleur@xxxxxxxxx) -->
<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('C:\Inetpub\wwwroot\development\shelco_company_registra
tion_form\xml\field.xml')/root"/>


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


	<xsl:template match="field">
		<xsl:copy>
			<xsl:copy-of select="@*" />
			<!-- If the field element contains a reference
attribute we need to first copy the element it is refering to -->
			<xsl:if test="$fieldCollection/field[
current()/@identity = @identity ]/@reference">
				<xsl:variable name="reference">
					<xsl:value-of
select="$fieldCollection/field[ current()/@identity = @identity
]/@reference"/>
				</xsl:variable>
				<xsl:copy-of
select="$fieldCollection/field[ @identity = $reference ]/@*" />
				<xsl:element
name="$fieldCollection/field[ @identity = $reference ]/*">
					<xsl:copy-of
select="$fieldCollection/field[ @identity = $reference ]/*" />
				</xsl:element>
			</xsl:if>
			<!-- Copy all elements and attributes from the
field collection -->
			<xsl:copy-of select="$fieldCollection/field[
current()/@identity = @identity ]/@*" />
			<xsl:element name="$fieldCollection/field[
current()/@identity = @identity ]/*">
				<xsl:copy-of
select="$fieldCollection/field[ current()/@identity = @identity ]/*" />
			</xsl:element>
			<!-- Also copy and override elements from the
field collection with the elements passed in -->
			<xsl:element name="current()/*">
				<xsl:copy-of select="current()/*" />
			</xsl:element>
		</xsl:copy>
	</xsl:template>


</xsl:stylesheet>


The file for $fieldCollection

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Author: Taco Fleur (tacofleur@xxxxxxxxx) -->
<root>
	<field
		name="bit"
		identity="5AB56CAB-D9DD-713A-D3EE45FAABF02F95"
		valid="01"
		type="boolean">
		<option value="0" />
		<option value="1" />
	</field>
	<field
		name="principalPlace"
		identity="5AB5F3AD-D9DD-713A-D582F669F41CFAA8"
		reference="5AB56CAB-D9DD-713A-D3EE45FAABF02F95" />
</root>

The XML file passed in

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Author: Taco Fleur (tacofleur@xxxxxxxxx) -->
<root>
	<form name="address">
		<segment value="01">
			<step value="a">
				<field

identity="5AB5F3AD-D9DD-713A-D582F669F41CFAA8"
					required="true"
					display="Is Principal Place of
Business" />
			</step>
		</segment>
	</form>
</root>

What I would like to end up with is


<root>
	<form name="address">
		<segment value="01">
			<step value="a">
				<field
					name="principalPlace"
<- Copied from variable

identity="5AB5F3AD-D9DD-713A-D582F669F41CFAA8" 	<- Copied from variable
					required="true"
<- Copied from file passed in
					display="Is Principal Place of
Business"		<- Copied from file passed in
					valid="01"
<- Copied from reference element in variable
					type="boolean">
<- Copied from reference element in variable
					<option value="0" />
<- Copied from reference element in variable
					<option value="1" />
<- Copied from reference element in variable
				</field>
			</step>
		</segment>
	</form>
</root>

Hope that makes sense and someone can help?

Thanks
________________________________

Taco Fleur - E-commerce Development Manager

Shelco Searches & Services

An Authorised ASIC Information Broker

www.shelco.com.au <outbind://8/www.shelco.com.au>

Ph: + 61 7 3236 2605

Current Thread