[xsl] Re:

Subject: [xsl] Re:
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 15 Apr 2002 15:13:45 +0100
Hi Biray,

> I need to select the first occurance of values in attributes of
> elements and show them in different color. i.e. for the following
> example lets say first occurance of "Mr.White", "Mr.Green",
> "Mr.Yellow", "Mr.Red" should be shown as red color and repeating
> ocuurances of these values should be shown as black color.

The most efficient way to tackle this problem is to use a key to index
all the Content elements by their ContentText attribute, as follows:

<xsl:key name="content" match="Content" use="@ContentText" />

You can then call the key using the key() function to get all the
Content elements with the same value for their ContentText attribute.
For example, to get all the Content elements with 'Mr.White' in their
ContentText attribute, you could use:

  key('content', 'Mr.White')

You can then identify which is the first of these nodes using a
positional predicate to pick the first one:

  key('content', 'Mr.White')[1]

So, given that you're currently on a Content element, you can find the
first Content element with that value for the ContentText attribute
using:

  key('content', @ContentText)[1]

You can then see if the Content element you're on is the same as this
Content element by comparing their generated IDs:

  generate-id() = generate-id(key('content', @ContentText)[1])

or using set logic:

  count(.|key('content', @ContentText)[1]) = 1

If they're the same, you want red; if they're different, you want
black, so you could use, for example:

<xsl:template match="Content">
  <div>
    <xsl:attribute name="style">
      <xsl:text>color: </xsl:color>
      <xsl:choose>
        <xsl:when test="count(.|key('content', @ContentText)[1]) = 1">
          <xsl:text>red</xsl:text>
        </xsl:when>
        <xsl:otherwise>black</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:value-of select="@ContentText" />
  </div>
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread