Skip to Main Content
XSLT
book

XSLT

by Doug Tidwell
August 2001
Intermediate to advanced content levelIntermediate to advanced
480 pages
11h 16m
English
O'Reilly Media, Inc.
Content preview from XSLT

Name

position() Function — Returns a number equal to the context position from the current context.

Synopsis

number position(
               )

Inputs

None.

Output

A number equal to the position of the current node in the evaluation context.

Defined in

XPath section 4.1, Node Set Functions.

Examples

This example uses the position() function to determine the background color of the rows of a table. The background colors cycle through the options white, darkgray, and lightgreen. Here’s the XML document we’ll use:

<?xml version="1.0"?>
<list>
  <title>A few of my favorite albums</title>
  <listitem>A Love Supreme</listitem>
  <listitem>Beat Crazy</listitem>
  <listitem>Here Come the Warm Jets</listitem>
  <listitem>Kind of Blue</listitem>
  <listitem>London Calling</listitem>
  <listitem>Remain in Light</listitem>
  <listitem>The Joshua Tree</listitem>
  <listitem>The Indestructible Beat of Soweto</listitem>
</list>

We’ll use this stylesheet to generate our HTML document:

<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title> <xsl:value-of select="/list/title"/> </title> </head> <body> <h1> <xsl:value-of select="/list/title"/> </h1> <table border="1"> <xsl:for-each select="/list/listitem"> <xsl:variable name="background-color"> <xsl:choose> <xsl:when test="position() mod 3 = 1">white</xsl:when> <xsl:when test="position() mod 3 = 2">darkgray</xsl:when> <xsl:otherwise>lightgreen</xsl:otherwise> </xsl:choose> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XSLT

Learning XSLT

Michael Fitzgerald
Inside XSLT

Inside XSLT

Steven Holzner
XSLT Cookbook

XSLT Cookbook

Sal Mangano
XSLT and XPATH: A Guide to XML Transformations

XSLT and XPATH: A Guide to XML Transformations

John Robert Gardner, Zarella L. Rendon

Publisher Resources

ISBN: 0596000537Supplemental ContentCatalog PageErrata