XSLT Position - How Does The XSLT Position Work With Examples
XSLT Position - How Does The XSLT Position Work With Examples
(https://www.educba.
com/software-
development/)
(https://www.educba.com/xslt- (https://www.educba.com/xslt-
json/) template/)
context node within the node list. Position() function is a recommended operation to return the
Syntax:
(https://www.educba.
The Syntax of the XSLT position is given as
com/software-
development/)
<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.
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>
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:
Even
</xsl:when>
</xsl:choose>
(https://www.educba.
com/software-
development/)
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
<xsl:template match="dailynews">
// 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>
<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:apply-templates>
</xsl:template>
This process each element internally with the position attribute in the apply-template format.
(https://www.educba.
Examples
com/software-
development/)
This section gives a code implementation to illustrate the Position() function with a different
example. Let’s get started,
Example #1
XML
<carom>
<coins>White</coins>
<coins>black</coins>
<coins>Pink</coins>
<coins>Yellow_striker</coins>
</carom>
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="coins">
(https://www.educba.
<xsl:value-of select="position()"/><br/>
com/software-
<xsl:apply-templates/>
development/)
</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
<Deadly_viruses>
<v_name>Corono Virus</v_name>
(https://www.educba.
<v_name>Ebola Virus</v_name>
com/software-
<v_name>Rabies</v_name>
development/)
<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
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Deadly-viruses">
</xsl:template>
<xsl:template match="v_name">
<xsl:text>. </xsl:text>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
Explanation
This code snippet works the same as the previous example The templates match an element
This code snippet works the same as the previous example. The templates match an element
(https://www.educba.
Output:
com/software-
development/)
Example #3
XML
<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>
(https://www.educba.
</Details>
com/software-
development/)
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
(https://www.educba.
Thecom/software-
index number of the node is retrieved in the above stylesheet which returns the odd
development/)
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>
(https://www.educba.
<commencement>16-Aug-2000</commencement>
com/software-
<batch score="10">WestIndies</batch>
development/)
<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:for-each select="ancestor::*">
</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
(https://www.educba.
com/software-
development/)
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>
</novel>
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
(https://www.educba.
<xsl:output method="html"/>
com/software-
<xsl:template match="/">
development/)
<html>
<head>
<title><xsl:value-of select="novel/paper"/></title>
</head>
<xsl:apply-templates select="novel"/>
</body>
</html>
</xsl:template>
<xsl:template match="novel">
<tr>
width="100" align="right">
<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>
<tr>
(https://www.educba.
<td>
com/software-
<xsl:value-of select="."/>
development/)
</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>
(https://www.educba.
<datapack xml:lang="en">
com/software-
development/) 1</paper>
<paper>title
<novellist>Frame 1</novellist>
<novellist>Frame 2</novellist>
<novellist>Frame 3</novellist>
</datapack>
</network>
XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<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">
(https://www.educba.
</xsl:if>
com/software-
<xsl:if test="position()=last()">
development/)
<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
Output:
(https://www.educba.
com/software-
development/)
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
1. XSLT apply-templates (https://www.educba.com/xslt-apply-templates/)
(https://www.educba.com/software-development/courses/software-development-course/?
btnz=edu-blg-inline-banner3)
(https://www.educba.
com/software-
development/)
About Us
Blog (https://www.educba.com/blog/?source=footer)
Sign Up (https://www.educba.com/software-development/signup/?
source=footer)
Contact Us (https://www.educba.com/contact-us/?source=footer)
Reviews (https://www.educba.com/software-development/reviews/?
source=footer)
Apps
iPhone & iPad (https://itunes.apple.com/in/app/educba-learning-
app/id1341654580?mt=8)
Android (https://play.google.com/store/apps/details?id=com.educba.www)
(https://www.educba.
com/software-
development/)
Resources
Certification Courses