[xsl] Grouping and converting by attribute by not using mode in templates

Subject: [xsl] Grouping and converting by attribute by not using mode in templates
From: "Ronan Phelan" <ro_spod@xxxxxxxxxxx>
Date: Thu, 17 Oct 2002 10:26:13 +1300
Hi

I have a problem trying to isolate groups of information in some xml. I'm trying to convert from Spanish to English, i.e. renaming fields, and using the name attribute as the tag with its value as the value tag (see below).
The field I want to convert is in the name attribute and the value of the field is in the value tag.


My problem is that the only way I can isolate each group (by the start of each field) is to use the "mode" attribute in the apply-templates.

Is there a more elegant solution to this problem, i.e. would it be easier to do it in two steps by converting the fields first? or is there a solution using keys?

Thanks in advance,

Ronan

==>Xml to convert

<fields>
	<field name="PersonaUnaCalle">
		<value>Sunny Street</value>
	</field>
	<field name="PersonaUnaCiudad">
		<value>Dublin</value>
	</field>
	<field name="PersonaUnaDOB">
		<value>19660301</value>
	</field>
	<field name="PersonaUnaTel">
		<value>01035312174234</value>
	</field>
		<field name="PersonaDosCalle">
		<value>Windy Street</value>
	</field>
	<field name="PersonaDosCiudad">
		<value>Cork</value>
	</field>
	<field name="PersonaDosDOB">
		<value>19800502</value>
	</field>
	<field name="PersonaDosTel">
		<value>0103532287213</value>
	</field>
</fields>


==>Xml converted


<request>
	<PersonOne>
		<address>
			<street> Sunny Street </street>
			<city> Dublin </city>
		</address>
		<dob> 19660301 </dob>
		<telephone> 01035312174234 </telephone>
	</PersonOne>
	<PersonTwo>
		<address>
			<street> Windy Street </street>
			<city> Cork </city>
		</address>
		<dob> 19800502 </dob>
		<telephone> 0103532287213 </telephone>
	</PersonTwo>
</request>


==>XSL using mode to isolate repeating groups


<xsl:template match="fields">
 <request>

<PersonOne>

<address>
<xsl:apply-templates select="field[contains(@name, 'Ciudad') and starts-with( @name, 'PersonaUna')]"/>
<xsl:apply-templates select="field[contains(@name, 'Calle') and starts-with( @name, 'PersonaUna')]"
<!-- more lines in the address to match fields -->
</address>


<xsl:apply-templates mode="other" select="field[starts-with( @name, 'PersonaUna')]"/>

</PersonOne>

<PersonTwo>

<address>
<xsl:apply-templates select="field[contains(@name, 'Ciudad') and starts-with( @name, 'PersonaDos')]"/>
<xsl:apply-templates select="field[contains(@name, 'Calle') and starts-with( @name, 'PersonaDos')]"/>
<!-- more lines in the address -->
</address>


<xsl:apply-templates mode="other" select="field[starts-with( @name, 'PersonaDos')]"/>

</PersonTwo>

</request>

</xsl:template>


<!-- address templates --> <xsl:template match="field[contains( @name, 'Calle')]"> <Street> <xsl:value-of select="."/> </Street> </xsl:template>

<xsl:template match="field[contains( @name, 'Ciudad')]">
	<city>
		<xsl:value-of select="."/>
	</city>
</xsl:template>
<!-- more template to match the lines in the address -->


<!-- all other fields -->
<xsl:template mode="other" match="field[starts-with( @name, 'PersonaUna')] | field[starts-with( @name, 'PersonaDos')]" >
<xsl:if test="contains( @name, 'DOB')">
<dob>
<xsl:value-of select="."/>
</dob>
</xsl:if>
<xsl:if test="contains( @name,'Tel')">
<telephone>
<xsl:value-of select="."/>
</telephone>
</xsl:if>
<!-- more if's to match other fields to match the lines that aren't address types -->
</xsl:template>



_________________________________________________________________
Get faster connections -- switch to MSN Internet Access! http://resourcecenter.msn.com/access/plans/default.asp



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread