This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: FIRST-OCCURENCE


Quoting Kim Durand <vsd18@rediffmail.com>:

[...]

> i run a for-each for colors and want to attach ***** to the text 
> of first occurence of the color with type = 'rude' and display the 
> subsequent color text in a normal way
> example,
> i want the display of the above xml file to be like ..
> *****red
> orange
> yellow


"rude" being one of those off-colors?   :-)
(sorry, couldn't resist)

Now that I've subjected you to the stupid pun I guess I owe 
you an answer.

Assuming you have a node set of the coloer elements you care about
in a variable named color-elements, then your for-each would look
like

    <xsl:for-each select="$color-elements">
      ...
    </xsl:for-each>

You already knew this but bear with me.

The first such color element whose type was 'rude' would be

    $color-elements[@type='rude'][1]

The function generate-id() returns a unique identifier for a node,
so we can determine if two nodes are the same node by seeing if
their generated ids are equal

    generate-id($node1) = generate-id($node2)

So, given the variable color-elements as described above

    <xsl:variable name="first-rude"
                  select="$color-elements[@type='rude'][1]"/>
    <xsl:for-each select="$color-elements">
        <xsl:if test="gener"ate-id(.) = generate-id($first-rude)">
            <xsl:text>*****</xsl:text>
        </xsl:if>
        <!-- output the color name.  normalize-space() removes
             leading and trailing whitespace. -->
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:for-each>

I've not tried this and I may be a bit rusty, but it will atleast
outline how to do this.



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]