Name
position()
Returns the position of the context item within the sequence of items currently being processed.
Syntax
[1.0] numberposition(
)
[2.0] xs:integerposition()
Inputs
None.
Output
A number equal to the position of the current node in the
sequence of items currently being processed. For example, if the
current node is the fifth <li>
being processed, position()
returns 5
.
Defined in
[1.0] XPath section 4.1, “Node Set Functions.”
[2.0] XQuery 1.0 and XPath 2.0 Functions and Operators section 16, “Context Functions.”
Examples
This example uses the position()
function to create a
style
attribute for the cells
of an HTML table. The background colors cycle through the options
black
, gray
, and white
, while the foreground colors cycle
through white
, white
, and black
. Here’s the XML document we’ll
use:
<?xml version="1.0"?>
<!-- albums.xml -->
<list xml:lang="en">
<title>Albums I've bought recently:</title>
<listitem>The Sacred Art of Dub</listitem>
<listitem>Only the Poor Man Feel It</listitem>
<listitem>Excitable Boy</listitem>
<listitem xml:lang="sw">Aki Special</listitem>
<listitem xml:lang="en-gb">Combat Rock</listitem>
<listitem xml:lang="zu">Talking Timbuktu</listitem>
<listitem xml:lang="jz">The Birth of the Cool</listitem>
</list>
We’ll use this stylesheet to generate our HTML document:
<?xml version="1.0"?>
<!-- position.xsl --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title> ...
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.