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> ...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.
Read now
Unlock full access