RE: [xsl] rearranging and colouring a document

Subject: RE: [xsl] rearranging and colouring a document
From: "Jacoby, Peter R." <PJACOBY@xxxxxxxxxxxx>
Date: Tue, 30 Jul 2002 15:46:53 -0400
Horst,

As you said there are two parts to your problem, sorting and adding a colour
attribute.  The first is simpler, using the <xsl:sort> element:

<xsl:apply-templates select="sp">
	<xsl:sort select="speaker" data-type="text" order="ascending" />
</xsl:apply-templates>

This will match on all <sp> elements and they will be sorted by the text in
the <speaker> element.

For the second part, you were not exactly clear on the format for storing
the possible colours to use so I took some liberty.  You can create a
separate document to store all possible colours you will use.  In order for
this method to work, you must have enough colours listed (equal to or more
than the number of unique <person> elements).

The contents of possibleColours.xml:
<?xml version="1.0" ?>
<colours>
	<colour>red</colour>
	<colour>blue</colour>
	<colour>green</colour>
</colours>

You can then use the document function to retrieve the contents of this
file:
<xsl:variable name="colours"
select="document('possibleColours.xml')/colours"/>

The way you match a colour with a unique person is up to you, but one
possibility is to use the position of the colour element and the text after
"person" in the speaker element (i.e. "person2" would match on the colour in
the second position).

The entire stylesheet is:

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

<xsl:variable name="colours"
select="document('possibleColours.xml')/colours"/>

<xsl:template match="text">
	<text>
		<xsl:apply-templates select="sp">
			<xsl:sort select="speaker" data-type="text"
order="ascending" />
		</xsl:apply-templates>
	</text>
</xsl:template>

<xsl:template match="sp">
	<xsl:param name="current" select="substring-after(speaker,
'person')" />
	<sp colour="{$colours/colour[position() = $current]}">
		<xsl:copy-of select="*|text()" />
	</sp>
</xsl:template>

<xsl:template match="text()"/>
	
</xsl:stylesheet>

Hope this helps.

-Peter 

[I hadn't realized how hard it is for an American to write colour instead of
color repeatedly.  And my spell checker complained too.]


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


Current Thread