0% found this document useful (0 votes)
217 views18 pages

XSLT Position - How Does The XSLT Position Work With Examples

The document provides examples and explanations of how to use the XSLT position() function. It begins with definitions and syntax, then provides 6 examples showing how position() can be used to return the position of nodes within a nodeset. Position() is demonstrated being used with if/when statements, in sorting and selecting nodes, and within for-each loops to manipulate processing based on a node's position.

Uploaded by

dkelic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views18 pages

XSLT Position - How Does The XSLT Position Work With Examples

The document provides examples and explanations of how to use the XSLT position() function. It begins with definitions and syntax, then provides 6 examples showing how position() can be used to return the position of nodes within a nodeset. Position() is demonstrated being used with if/when statements, in sorting and selecting nodes, and within for-each loops to manipulate processing based on a node's position.

Uploaded by

dkelic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

XSLT position

(https://www.educba.
com/software-
development/)

 (https://www.educba.com/xslt-  (https://www.educba.com/xslt-
json/) template/)

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:

Start Your Free Software Development Course


Web development, programming languages, Software testing & others

(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.

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>
(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 &lt;6 and

mod2=1 for the daily news, hope this works.

<xsl:template match="dailynews">

<xsl:if test="positon() &lt;= 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.
(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

<?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">

(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

<?xml version="1.0" encoding="UTF-8"?>

<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

<?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
This code snippet works the same as the previous example. The templates match an element

and return the even value as position.

(https://www.educba.
Output:
com/software-
development/)

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>

(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: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:

(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>

<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">

(https://www.educba.
<xsl:output method="html"/>

com/software-
<xsl:template match="/">

development/)
<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() &gt; 2]">

<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>

<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>

(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

conditional statements to return a value.

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

articles to learn more –

1. XSLT apply-templates (https://www.educba.com/xslt-apply-templates/)

2. XSLT Loop (https://www.educba.com/xslt-loop/)

3. XSLT substring-before (https://www.educba.com/xslt-substring-before/)

4. XSLT Transformation (https://www.educba.com/xslt-transformation/)

ALL IN ONE SOFTWARE DEVELOPMENT BUNDLE


(600+ COURSES, 50+ PROJECTS)
 600+ Online Courses
 50+ projects
 3000+ Hours
 Verifiable Certificates
 Lifetime Access
Learn More
Learn More

(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)

Who is EDUCBA? (https://www.educba.com/about-us/?source=footer)

Sign Up (https://www.educba.com/software-development/signup/?
source=footer)

Corporate Training (https://www.educba.com/corporate/?source=footer)

Certificate from Top Institutions (https://www.educba.com/educbalive/?


source=footer)

Contact Us (https://www.educba.com/contact-us/?source=footer)

Verifiable Certificate (https://www.educba.com/software-


development/verifiable-certificate/?source=footer)

Reviews (https://www.educba.com/software-development/reviews/?
source=footer)

Terms and Conditions (https://www.educba.com/terms-and-conditions/?


source=footer)

Privacy Policy (https://www.educba.com/privacy-policy/?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

Free Courses (https://www.educba.com/software-development/free-courses/?


source=footer)

Java Tutorials (https://www.educba.com/software-development/software-


development-tutorials/java-tutorial/?source=footer)

Python Tutorials (https://www.educba.com/software-development/software-


development-tutorials/python-tutorial/?source=footer)

All Tutorials (https://www.educba.com/software-development/software-


development-tutorials/?source=footer)

Certification Courses

All Courses (https://www.educba.com/software-development/courses/?


source=footer)

Software Development Course - All in One Bundle


(https://www.educba.com/software-development/courses/software-
development-course/?source=footer)

Become a Python Developer (https://www.educba.com/software-


development/courses/python-certification-course/?source=footer)

Java Course (https://www.educba.com/software-development/courses/java-


course/?source=footer)

Become a Selenium Automation Tester (https://www.educba.com/software-


development/courses/selenium-training-certification/?source=footer)

Become an IoT Developer (https://www.educba.com/software-


development/courses/iot-course/?source=footer)

ASP.NET Course (https://www.educba.com/software-development/courses/asp-


net-course/?source=footer)

VB.NET Course (https://www.educba.com/software-development/courses/vb-


net-course/?source=footer)

PHP Course (https://www.educba.com/software-development/courses/php-


course/?source=footer)

You might also like