EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials XML Tutorial XPath First Element
 

XPath First Element

XPath First Element

Definition of XPath First Element

XPath first element is used to locate the first element from the specified string. It is the predefined method of the language of XPath that is used in a statement of XPath for locating the node of the first element from the node type. Basically, the first element method in XPath will select the first element out of the all element which was present in a string for the first element of XPath.

 

 

What is the XPath first element?

  • First element is returning the position of the first element from the current context. Sometimes we need to find which element is coming to first and which was next, for finding the first element from the set we are using XPath first element.
  • First element is very important for defining the template for the first occurrence for the given element or it is also useful for testing a given node from the first in a set of nodes to which it belongs.
  • The first element will be returning a number from size as per the context evaluation of the expression.
  • We can find the first element by using the following different ways are as follows.
  • Find the first element by name.
  • Find the first element by id.
  • Find the first element by class name.
  • Find the first element by XPath.
  • Find the first element by link text.
  • Find the first element by CSS selector.
  • Find the first element by tag name.
  • Find the first element by partial link text.
  • In order to get the first element of an ID or name of an element, we can use the XPath to display the first value of an element.

How to XPath the first element?

  • The below steps show how we can XPath the first element are as follows. We are creating the project name as XPathFirstElement. In the below step we are creating the project template of first element in spring boot.
  1. At the time of creating the project we are providing project group name as com. example, artifact name as XPathFirstElement, project name as XPathFirstElement, and selected java version as 11. We are defining the version of spring boot as 2.6.7.

Group – com.example

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Artifact name – XPathFirstElement

Name – XPathFirstElement

Spring boot – 2.6.7

Project – Maven

Java – 11

Package name – com.example. XPathFirstElement

Project Description – Project for XPathFirstElement

Dependencies – Spring web

Packaging – Jar

1-1

2. In this step we are extracting the downloaded project and opening the same by using the spring tool suite as follows.

1-2

3. In this step we are checking all the project structures and their files are as follows. Also, we are checking the pom.xml file is created or not. Suppose this file is not created then we need to create the same manually. In the below example this file is created, so we have no need to create it manually.

4

4. In this step we are adding a dependency in the first element project. We are adding dependencies as follows.

Code –

<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>

5

5. Below is the view of first element from the element array finder. It will not retrieving the underlying element.

Code –

<ul class = "letter">
<li> A </li>
<li> B </li>
<li> C </li>
<li> D </li>
<li> E </li>
<li> F </li>
<li> G </li>
<li> H </li>
<li> I </li>
<li> J </li>
<li> K </li>
<li> L </li>
<li> M </li>
<li> N </li>
<li> O </li>
<li> P </li>
<li> Q </li>
<li> S </li>
<li> T </li>
<li> U </li>
<li> V </li>
<li> W </li>
<li> X </li>
<li> Y </li>
<li> Z </li>
</ul>

6

6. Below is the code of XPath first element from the element array finder.

Code –

public class FirstElement {
let elefirst = element.all (by.css('.items li')).first();
expect (elefirst.getText ()).toBe ('First')
}

XPath First Element 7

XPath First Element 8

Enable XPath selection

  • Google chrome provides the debugging tool which was built called chrome dev tools. It will include many handy features which were used to evaluate the CSS selector’s without any extensions of third-party tools.
  • We can also execute the token from the console panel which was used to validate and evaluate the same.
  • In the below example we are enabling the XPath selection as follows. We are enabling the selection in the following example.

XPath First Element 9

  • XPath uses the expression of the path for selecting a node in the xml document. Nodes are selected by using the following path. Below is the most useful expression of XPath selection as follows.
  • Nodename – It will select all the nodes with the name as node name.
  • / – This is used to select from the root node.
  • // – This symbol is used to select the nodes from the current node which will match the selection
  • . – This is used to select the current node.
  • @- It is used to select the attribute.
  • We can use the search function inside the elements panel for evaluating the CSS selector’s XPath and also we can highlight the matching rows from the DOM.

XPath First Element Array

  • XPath first element array can be nested, we can say that members of an array can be attribute values including the sequences, function, maps and other values. The array member contains a sequence that is empty.
  • The first element of an array contains a single item, so it contains the sequence of an array.
  • As per sequence, the members of an array are addressed by using an integer in the range of 1 to N.
  • XPath’s first element array is XDM values which are immutable. At the time of appending, removing an entry from an array we will get a new array.
  • By using the sequence and maps first element array doesn’t have an intrinsic type in an element array.
  • Below is the example.

Code –

(/Students/stud[@name='PQR'])[1]

XPath First Element 10

Conclusion

The XPath first element is returning the position of first element from the current context. XPath first element is the predefined method of language of XPath is used in a statement of XPath for locating the node of first element from the node type.

Recommended Articles

This is a guide to XPath First Element. Here we discuss the Definition, What is the XPath first element, How to XPath the first element , examples with code implementation respectively. You may also have a look at the following articles to learn more –

  1. XPath finder
  2. XPath class
  3. XPath attribute
  4. XPath text

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW