[xsl] Re: a nicer total sibling count than this

Subject: [xsl] Re: a nicer total sibling count than this
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 18 Sep 2002 12:58:34 -0700 (PDT)
--- scruss at sympatico dot ca wrote:

> 
> I'm working with a dictionary marked up in XML. Conventionally, one
> numbers definitions (def elements here)  only if there are two or
> more
> definitions for the given part of speech. 
> 
> To my surprise and delight, the following worked, producing heavy def
> numbers with a following non-break space:
> 
>  <xsl:template match="def">
>   <xsl:if test="(count(preceding-sibling::def) +   
>                  count(following-sibling::def)) >= 1">
>    <xsl:element name="strong">
>     <xsl:number/>
>     <xsl:text> </xsl:text>
>    </xsl:element>
>   </xsl:if>
>   <xsl:apply-templates/>
>  </xsl:template>
> 
> Is there a prettier/more efficient way of doing the same thing
> without
> resorting to frankly ugly
> '(count(preceding-sibling::def)+count(following-sibling::def))'?
> 
> thanks,
>  Stewart


Hi Stuart,

count(preceding-sibling::def) +   
                  count(following-sibling::def) >= 1

is equivalent to:

preceding-sibling::def or following-sibling::def

The latter may be significantly optimised by a clever XSLT processor,
because:

1. for either of 
    preceding-sibling::def 
and
    following-sibling::def

it is only necessary to see that a single (at least one) node is
returned by the corresponding expression. This contrasts sharply with
using count(), in which case all nodes in a (potentially long) node-set
have to be counted.

2. If preceding-sibling::def has at least one node, then there's no
need at all to evaluate (the other operand of "or") 
following-sibling::def




__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com

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


Current Thread