Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
This chapter is from the book

Testing the Position

In addition to applying conditions to location paths, you can actually choose to select a specific node in the node set: the first, second, or even the last.

To test a node’s position:

  • Type position() = n, where n is the number that identifies the position of the node within the current node set (Figure 4.3).
    x s l t
     <p>These ancient wonders are
      <xsl:for-each select=
       <xsl:value-of select="."/>
        <xsl:when test=
        <xsl:when test=
          "position()=last()-1">, and
        <xsl:otherwise>, </xsl:otherwise>

    Figure 4.3. Here I am using position functions to format a sentence that lists all the wonders. The wonder’s name is output no matter what. If it’s in the last position, a period is also output after the name. If it’s in the second-to-last position (position() = last()–1), a comma, the word and, and a space are output after the name. Otherwise, if it’s in any other position, only the name and a space are output.

Figure 4.4

Figure 4.4 The output from the transformation based on Figure 4.3 shows the list of the seven wonders, well-formatted with commas separating the names and a final and before the last wonder.

To find the last node in a node set:

  • Type last() to get the last node.
  • + Share This
  • 🔖 Save To Your Account