XPath Axis: Enhance your skills by practicing XPath Child,Descendent,Descendant-or-self,Following axis syntax with our given sample HTML codes and interactive exercises, designed to help you master selecting elements in HTML and XML documents.

Boost Your Skills: Practice XPath Syntax with easy Exercises.


Home->Practice 9

  Next Exercise >>
Please Follow Step 1: Read XPath Question >>Step 2: Check HTML Source Code >>Step3: Enter XPath Syntax in O/P Box
XPath Exercise 9

Q1. Find all Child Text from Div ID="elephant"
using Child axis.

Q2. Find all Child Paragraph Text from Div ID="malewildanimal"
using Child axis.

Q3. Find all Child Paragraph Text from Div ID="animal"
using Child axis.

Q4. Find all Descendent Paragraph Text from Div ID="animal"
using Descendent axis.

Q5. Find all Descendent Paragraph Text from Div ID
="malewildanimal".
using Descendent axis.

Q6. Find all Paragraph Text from Div ID
="malewildanimal"
using Descendant-or-self axis.

Q7. Find all Paragraph Text from Div ID
="lion"
using Following axis.

Q8. Find all Paragraph Text from Div ID
="lion"
using Following axis.

HTML Source document
        <div id="animal">
  <p>Animal</p>
  
  <div id="wildanimal">
    <p>Wild Animal</p>
    
	<div id="malewildanimal">
      <p>Male Wild Animal </p>
            
                <div id="lion">
                <p>Lion</p>
		        </div>
           
                <div id="elephant">
                <p>Elephant</p>
                    <div id="elephant1">
			        <p>Elephant 1</p>
			        </div>
			 	
			        <div id="elephant2">
			        <p>Elephant 2</p>
			         </div>
			    </div>
            
      </div>

</div>
</div>
        
        

XPath O/P Box

Enter XPath Syntax here:   

XPath Tips and Syntax For This Page Exercise :

Using Child Axis

//child::tagName

e.g

//div[@class='Mammal']/child::div

The child axis in XPath is used to select the direct children of an element. The child axis is abbreviated as / and can be used in an XPath expression to select elements that are directly nested within the current context element.

Using Descendent Axis

e.g

//div[@class='Animal']/descendant::div

The descendant axis in XPath is used to select elements that are descendants (children, grandchildren, etc.) of the current context element. The descendant axis is abbreviated as // and can be used in an XPath expression to select elements that are nested within the current context element, regardless of the level of nesting.

Using Descendant-or-self Axis

XPath(Current node): //div[@class = 'signup_form new']//descendant-or-self::div

The descendant-or-self axis in XPath is used to select elements that are descendants (children, grandchildren, etc.) of the current context element, as well as the current context element itself. The descendant-or-self axis is abbreviated as ./ or simply . and can be used in an XPath expression to select elements that are nested within the current context element, as well as the current context element itself, regardless of the level of nesting.

Using Following Axis

//div[@class='Mammal']/following::div

The following axis in XPath is used to select elements that appear after the current context element, within the same parent element. The following axis is abbreviated as following:: and can be used in an XPath expression to select elements that are siblings of the current context element, and that appear after it in the document order.

We would love to hear your thoughts, suggestions, concerns or problems with anything so we can improve. Request you to please use Social Media button in Right Side for this website growth.