Re: [xsl] Coma delimited values in attribute

Subject: Re: [xsl] Coma delimited values in attribute
From: andrew welch <andrew.j.welch@xxxxxxxxx>
Date: Sat, 17 Sep 2005 10:16:14 +0100
If you can use 2.0 (or 1.0 + extensions) then I would do it in 2
stages - the first stage sorts out the markup into a better structure.
 The second stage is really simple then.

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

<xsl:variable name="betterMarkup">
	<xsl:for-each select="//field">
		<field>
			<xsl:for-each select="tokenize(@value, ',')">
				<value><xsl:value-of select="."/></value>
			</xsl:for-each>
		</field>
	</xsl:for-each>
</xsl:variable>

<xsl:template match="Fields">
	<table>
		<xsl:apply-templates select="$betterMarkup"/>
	</table>
</xsl:template>

<xsl:template match="field">
	<tr>
		<xsl:apply-templates/>
	</tr>
</xsl:template>

<xsl:template match="value">
	<td>
		<xsl:apply-templates/>
	</td>
</xsl:template>

</xsl:stylesheet>


Here the variable $betterMarkup reorganises the input into a better
structure:

<field>
	<value>tarif1</value>
	<value>tarif2</value>
	<value>tarif3</value>
	<value>tarif4</value>
</field>
<field>
	<value>str</value>
	<value>cur</value>
	<value>str</value>
	<value>cur</value>
</field>
<field>
	<value>1</value>
	<value>1</value>
	<value>1</value>
	<value>1</value>
</field>
<field>
	<value>152</value>
	<value>100</value>
	<value>252</value>
	<value>203</value>
</field>

The second stage then does an apply-templates on that structure.

If you wanted to do this in 1.0 you will need the node-set extension,
and optionally the tokenize extension (you could do it recursively)

cheers
andrew

Current Thread