Expression values

The value to insert can be calculated using an expression, which is entered into the Value attribute. The expression must return a numeric value, as this is used as the count value. The most common expression seen here is simply 'position()'. This function returns the position of the current element from among its siblings:

<number value="position()" format="A"/>

Under most conditions, adopting this approach has no effect on the result, but when the items are also sorted (see Chapter 9), and the final sorted list has to be numbered sequentially, this is the only way to achieve the desired effect. This is due to the fact that the expression is applied to the final, sorted list and not to the items in their original document order:

   <list>
     <item>Z</item>
     <item>Y</item>
     <item>X</item>
   </list>


<xsl:template match="list">
  <DIV>
    <xsl:apply-templates><xsl:sort/></xsl:apply-templates>
  </DIV>
</xsl:template>

<xsl:template match="item">
  <P>
    <xsl:number value="position()" format="i) "/>
    <xsl:apply-templates/>
  </P>
</xsl:template>


   <DIV>
      <P>i) X</P>
      <P>ii) Y</P>
      <P>iii) Z</P>
   </DIV>

Even when not sorting, this technique raises some interesting new possibilities. For example, it is possible to create a reverse list by deducting the current position from the total number of items:

   <list>
     <item>first</item>
     <item>second</item>
     <item>third</item>
   </list>


<number value="last() + 1 - position()" />


   <DIV>
     <P>3) first</P>
     <P>2) second</P>
     <P>1) third</P>
   </DIV>

See Chapter 20 for more information on expressions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset