Intro to XPath -1



XPath
            XML Path used to navigate through to identify the elements / attributes in XML document / HTML document


To understand XPath little bit knowledge of HTML is required
  • XPath uses path expressions to navigate in XML / HTML documents
  • XPath is a language to find Elements
  • XPath also contains a library of standard functions used to identify elements
Sample HTML Code

           
                       
                                   
                                               
……
                                               
……
                                               
……
                                   
                                               
……
                                               
……
                                               
……
                                   
                       
           

In the above code to refer td in 2nd tr the XPath will look like below

Xpath=/html/body/table/tr[2]/td[1]
or
Xpath=//*[@id=’t1’]/x:tr[2]/x:td[1]
or
Xpath=//table[@id=’t1’]/x:tr[2]/x:td[1]


Sample 2 XML File
  satish
  6000
            
  sarath
  5000
            

  kiran
  8000
            

The below xpath will get all ename elements starting with “sa”
Xpath=//ename[starts-with(.,’sa’)

The below xpath get only ename element  having text “kiran”
Xpath=//ename[text()=”kiran”]


The below xpath will get all emp having sal >5000
            Xpath=//emp[sal>5000]

Comments