Updated April 7, 2023
Definition of XSLT position
XSLT position is a useful piece of function that returns the exact position from the current context node within the node list. Position() function is a recommended operation to return the node position that is being processed.
Syntax:
The Syntax of the XSLT position is given as
<xsl:when test= "position()=n>
</xsl:when>
If the position=1, the first element in the XML file returns a position of 1. Similarly, it varies for the number.
How does the XSLT position work?
The Position() function works by applying few conditions to the Xpath by choosing a specific node.
Position() = n, N is the number to find the position of the node within the nodeset.
With respective to if() test the ranges falls from 1 to last() to get the position of the element.
<xsl:if test="position() = 1">
First page in the book
<xsl:value-of select="." />
</xsl:if>
Using modulo function to determine the position. We need to use products bounding so that the result will be in a n product of a row. So modulo could be used like:
<xsl:when test="position() mod 2 = 0">
Even
</xsl:when>
<xsl:when test="position() mod 2 = 1">odd</xsl:when>
</xsl:choose>
To do the generic position on the functionality daily news class Position() is used in the selected node-set so that a fresh position counter is revealed. For instance, to set the position <6 and mod2=1 for the daily news, hope this works.
<xsl:template match="dailynews">
<xsl:if test="positon() <= 6">
// code
</XSL:template>
Next to find the last node in a set we can use position along with the keyword last ().
<xsl:choose>
<xsl:when test= "position()=last()">.</xsl:when>
<xsl:when test= "position()=last()-1">, and
</xsl:when>
<xsl:otherwise>, </xsl:otherwise>
</xsl:choose>
With the apply template and with few attributes as input the input may process like
<xsl:template match="element3">
<xsl:apply-templates select="../ndeelement1/childnode/*">
<xsl:with-param name="aaa_b" select="position() - 1"/>
<xsl:with-param name="x_id" select="@eid"/>
</xsl:apply-templates>
</xsl:template>
This process each element internally with the position attribute in the apply-template format.
Examples
This section gives a code implementation to illustrate the Position() function with a different example. Let’s get started,
Example #1
XML
<?xml version="1.0" encoding="UTF-8"?>
<carom>
<coins>White</coins>
<coins>black</coins>
<coins>Pink</coins>
<coins>Yellow_striker</coins>
</carom>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1> Sample Carrom List</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="coins">
<xsl:value-of select="position()"/><br/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="carom">
<xsl:apply-templates select="*"/>
</xsl:template></xsl:stylesheet>
Explanation
When the above XSLT file applied to XML it maps the element ‘coin’ whose content holds the position in the order from 1 to 4 as there are 4 coin elements. And the Output is shown in the HTML format.
Output:
Example #2
XML
<?xml version="1.0" encoding="UTF-8"?>
<Deadly_viruses>
<v_name>Corono Virus</v_name>
<v_name>Ebola Virus</v_name>
<v_name>Rabies</v_name>
<v_name>Hanta virus</v_name>
<v_name>Small pox</v_name>
<v_name>Influenza</v_name>
<v_name>Dengue</v_name>
<v_name>Sars-CoV</v_name>
</Deadly_viruses>
XSL
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="Deadly-viruses">
<xsl:apply-templates select="v_name" />
</xsl:template>
<xsl:template match="v_name">
<xsl:value-of select="position()" />
<xsl:text>. </xsl:text>
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Explanation
This code snippet works the same as the previous example. The templates match an element and return the even value as position.
Output:
Example #3
XML
<?xml version="1.0" encoding="UTF-8"?>
<Details>
<Warehouse>
<WName>Google</WName>
</Warehouse>
<Warehouse>
<WName>Yahoo</WName>
</Warehouse>
<Warehouse>
<WName>Starfish</WName>
</Warehouse>
<Warehouse>
<WName>DTH</WName>
</Warehouse>
<Warehouse>
<WName>Fishnet</WName>
</Warehouse>
</Details>
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Warehouse">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="wid">
<xsl:number/>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Explanation
The index number of the node is retrieved in the above stylesheet which returns the odd number as an index.
Output:
Example #4
XML
<?xml version="1.0"?>
<test part="Win">
<game>
<commencement>1-July-99</commencement>
<batch score="5">Russia</batch>
<batch score="6">China</batch>
</game>
<commencement>12-April-96</commencement>
<game>
<batch score="4">Denmark</batch>
<batch score="3">Sweden</batch>
</game>
<game>
<commencement>16-Aug-2000</commencement>
<batch score="10">WestIndies</batch>
<batch score="9">Brazil</batch>
</game>
</test>
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:comment>
<xsl:value-of select="attribute()" />
<xsl:for-each select="ancestor::*">
<xsl:sort select="position()" order="ascending" />
<xsl:text> date scheduled on </xsl:text>
<xsl:value-of select="attribute()" />
</xsl:for-each>
</xsl:comment>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
Explanation
Having an XML file above with the game elements we need to display the position of an element in a sort function in ascending order. Therefore the output is numbered with 5,4 and so on by retrieving the text content along with the function.
Output:
Example #5
XML
<?xml version="1.0"?>
<novel xml:lang="en">
<paper>title 1</paper>
<novellist>Essay 1</novellist>
<novellist>Essay 2</novellist>
<novellist>Essay 3</novellist>
<novellist xml:lang="fr">Essay 4</novellist>
<novellist xml:lang="de">Essay 5</novellist>
<novellist xml:lang="za">Essay 6</novellist>
<novellist xml:lang="es">Essay 7</novellist>
</novel>
XSL
<?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="novel/paper"/></title>
</head>
<body style="font-family: sans-serif;">
<xsl:apply-templates select="novel"/>
</body>
</html>
</xsl:template>
<xsl:template match="novel">
<table border="1.1" cellpadding="4" cellspacing="4">
<tr>
<td style="background: yellow; color: white;
font-weight: bold; font-size: 120%;"
width="100" align="right">
<xsl:if test="count(novellist) > 2">
<xsl:attribute name="rowspan">
<xsl:value-of select="count(novellist)"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="paper"/>
</td>
<td>
<xsl:value-of select="novellist[2]"/>
</td>
</tr>
<xsl:for-each select="novellist[position() > 2]">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Explanation
The above example returns a value with an XSL:for-each as they provide access to novelist element by the position() to return a value greater than 2. The XSLT template uses the Position() function to manipulate the number of Essays being processed, concerning other nodes in the list.
Output:
Example #6
XML
<?xml version="1.0"?>
<network>
<datapack xml:lang="en">
<paper>title 1</paper>
<novellist>Frame 1</novellist>
<novellist>Frame 2</novellist>
<novellist>Frame 3</novellist>
<novellist> Frame 4</novellist>
<novellist> Frame 5</novellist>
<novellist> Frame 6</novellist>
<novellist> Frame 7</novellist>
</datapack>
</network>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Data Frame</h2>
<p>Packets
<xsl:for-each select="network/datapack">
<xsl:value-of select="novellist"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()-2">
<xsl:text> and </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template></xsl:stylesheet>
Explanation:
In the above code snippet, we have examined the last () function in the position to find the exact location of the list by using the for-each statement. By then the frame numbers are returned in the result as a result of the last value also we have achieved using various conditional statements to return a value.
Output:
Conclusion
Coming to an end, we have seen a way to use the Position() function to retrieve the position number from the code which is given in an easy way to understand and well structured. And also done with lots of examples to show how the function works. Therefore, we have explored the full power of the function to understand the concept and to work in various applications.
Recommended Articles
This is a guide to XSLT position. Here we discuss definition, syntax, How does the XSLT position work? examples with code implementation. You may also have a look at the following articles to learn more –