This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: rearranging and colouring a document
- From: horst at freedict dot de
- To: xsl-list at lists dot mulberrytech dot com
- Date: Thu, 8 Aug 2002 19:59:01 +0200 (CEST)
- Subject: Re: [xsl] rearranging and colouring a document
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hello,
thanks for your advice, I managed to get the sorting to work. It
took a while, as the fast sablotron seems to have problems with
the solution, but xalan works find for me.
However, I tried the proposed solution for colouring, but had
problems, as my description of the source was imprecise. Here a
more suitable example:
<text>
<sp><speaker>foo</speaker><p>text 1</p></sp>
<sp><speaker>bar</speaker><p>text 2</p></sp>
<sp><speaker>foo</speaker><p>text 3</p></sp>
<sp><speaker>xxxx</speaker><p>text 4</p></sp>
<sp><speaker>foo</speaker><p>text 5</p></sp>
<sp><speaker>xxxx</speaker><p>text 6</p></sp>
<sp><speaker>bar</speaker><p>text 7</p></sp>
</text>
how could I extract some kind of index from the speaker element?
- that is in this example:
foo: 1
bar: 2
xxxx: 3
(or any other unique number sequence suitable for indexing).
So it can be mapped to colours, which for example can be found in
an external xml document.
Thanks,
Horst
On 30 Jul, Jacoby, Peter R. wrote:
> 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
>
--
Horst@freedict.de
Horst Eyermann
Germany
You need a dictionary? - visit http://www.freedict.de
for free (GPL) dictionaries (unix; windows work in progress)
For windows, visit http://www.freedict.de/wbuch
A article (in German) about dictionary efforts on the net
http://www.heise.de/tp/deutsch/inhalt/on/5927/1.html
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list