Updated March 28, 2023
Definition on XPath Last Element
XPath last element is a method that is used to locate the last element from a string. Basically, the last element method in XPath will select the last element out of all element which was present in a string. XPath’s last element is the predefined method of XPath language which was used in a statement for locating the node of the last element from the specified node type.
Overview of XPath Last Element
The last element will be returning a number from size as per the evaluation context of expression. The last element is returning the position of last element from the current context. Last element function is very important and useful for defining the template for the last occurrence for the given element or it is also useful for testing of a given node from the last in a set of node which was it belongs. Sometime we need to find which element will first and last, for finding last element from the set same time we are using the last element.
How can I use XPath last element?
- Below steps shows how we can use the XPath last element are as follows. We are creating the project name as XPathLastElement.
1. In this step we are creating the project template of XPath last element in spring boot. We have providing project group name as com.example, artifact name as XPathLastElement, project name as XPathLastElement and selected java version as 11. We are defining the version of spring boot as 2.6.7.
Group – com.example
Artifact name – XPathLastElement
Name – XPathLastElement
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name – com.example. XPathLastElement
Project Description – Project for XPathLastElement
Dependencies – Spring web
Packaging – Jar
2. In this step we are extracting the downloaded project and opening the same by using spring tool suite are as follows.
3. In this step we are checking all the project structure and its files are as follows. Also, we are checking that pom.xml file is created or not. If suppose this file is not created then we need to create the same manually. In below example this file is created, so we have no need to create it manually.
4. In this step we are adding the dependency in XPath last element project. We are adding dependency are as follows.
Code –
<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>
5. Now we are creating the xml file to find the last element, our xml file contains the below code are as follows.
Code –
<list>
<element stud_name = "A" />
<element stud_name = "B" />
<element stud_name = "C" />
<element stud_name = "D" >
<element stud_name = "D" />
<element stud_name = "E" />
<element stud_name = "F" />
<element stud_name = "G" />
</element>
<element stud_name = "H" />
<element stud_name = "I" />
</list>
6. Now we are creating the xsl file to find the last element, our xsl file contains the below code are as follows.
Code –
<?xml version = '1.0'?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://example.com/example">
<xsl:output method = "xml" omit-xml-declaration = "yes"/>
<xsl:template match = "text()"/>
<xsl:template match = "*">
<xsl:element stud_name = "{name()}">
<xsl:apply-templates select = "*|@*"/>
<xsl:if test = "text()">
<xsl:value-of select = "."/>
</xsl:if>
</xsl:element>
</xsl:template>
…….
</xsl:stylesheet>
XPath Last Element Function
- To find the last element we are using XPath last element function. This operation basically performed by using query language, but in XPath we have no need to separate query.
- Below example shows XPath last element function as follows. We are implementing the same by using html page.
- First we are opening the URL which was we are showing the working of XPath function.
- After opening the URL we are inspecting a web page as follows. We can see that after inspecting the text it will showing below html code.
- The last function will returns the number of equal’s context size from the equivalent expression of context.
- Below is the syntax of XPath last element function are as follows.
Syntax –
Last()
- The above function will returning the number of equal in a size of context from the context of evaluation expression.
- If suppose we want to describe the set of nodes from the document from the XPath expression for the single step then we are getting the predicate by adding those expression. Also we can find out last by adding the predicate as a last.
- To get the attribute of element by using the smallest and greatest value or we can sort the nodes by using any options of sorting and need to use the same predicates to pick out from their end.
XPath Last Element Examples
- In below example we are illustrating that how we can use the last function to select the last element which was contained into the following document of XML. Below example shows to create the xml file are as follows.
Code –
<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test (x+)>
<!ELEMENT x (x+| y+)>
<!pTTLIST x
p ID #REQUIRED>
<!ELEMENT y pNY>
]>
<test>
<x p = "p11">
<x p = "p21">
<x p = "p31">
<y> y31 </y>
<y>y32</y>
</x>
</x>
</x>
<x p = "p12">
<x p = "p22">
<y> y21 </y>
<y> y22 </y>
</x>
</x>
<x p = "p13">
<y> y11 </y>
<y> y12 </y>
</x>
<x p = "p14">
<y> y03 </y>
<y> y04 </y>
</x>
</test>
- Now we are creating the xsl file to find the last element, our xsl file contains the below code are as follows.
Code –
<?xml version = '1.0'?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://example.com/example">
…….
</xsl:stylesheet>
Conclusion
XPath last element function is very important and useful for defining the template for the last occurrence of the given element. Basically last element method in XPath will selecting the last element out from the all element which was present in a string.
Recommended Articles
This is a guide to XPath Last Element. Here we discuss the Definition, Overviews, How can I use XPath last element, and examples with code implementation respectively. You may also have a look at the following articles to learn more –