Re: [xsl] How to get a "heading" from an element

Subject: Re: [xsl] How to get a "heading" from an element
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 10 Feb 2002 17:24:16 +0000
Hi Roger,

>>You can compare its airline-name with this airline's airline-name
>>with:
>>
>>  preceding-sibling::airline[1]/airline-name != airline-name
[snip]
> Thanks for this! I had just one problem, ie the very first
> airline-name didn't come out, so I changed the above to:
>
> <xsl:variable name="var-num"><xsl:number/></xsl:variable>
> <xsl:if test="$var-num = 1">
>     <tr>
>       <td><xsl:value-of select="airline-name" /></td>
>     </tr>
> </xsl:if>
>  <xsl:if test="preceding-sibling::airline[1]/airline-name !=
airline-name">>
>     <tr>
>       <td><xsl:value-of select="airline-name" /></td>
>     </tr>
>   </xsl:if>
>   <tr>
>     <td><xsl:value-of select="airport-code" /></td>
>     <td><xsl:value-of select="airport-name" /></td>
>   </tr>
>
> It works, but is there a better way?

Whoops, sorry about that! You can do what you have done, of course,
but using xsl:number is pretty inefficient, and it's a bit of a waste
having two separate xsl:ifs that generate exactly the same result when
you could combine the two conditions with an 'or'.

I recommend you either do:

  <xsl:if test="position() = 1 or
                preceding-sibling::airline[1]/airline-name !=
                airline-name">
    ...
  </xsl:if>

Or, possibly better:

  <xsl:if test="not(preceding-sibling::airline[1]/airline-name =
                    airline-name)">
    ...
  </xsl:if>

This highlights the difference between A != B and not(A = B). The A !=
B checks whether there *is* a preceding sibling airline element whose
airline-name *isn't* the same as this one's airline-name. The not(A =
B) checks whether there *isn't* a preceding sibling airline element
whose airline-name *is* the same as this one's airline-name. So the
latter includes the first airline element (since it doesn't have a
preceding sibling airline at all, let alone one whose airline-name is
the same as this one's). Whereas the former doesn't include the first
airline element.

Sorry about my mistake,

Jeni

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


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


Current Thread