Skip to content
Nadeem Lughmani edited this page Apr 17, 2021 · 1 revision

XPath

XPath stands for XML Path Language. XPath can be used to navigate through elements and attributes in an XML document. In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes. XML documents are treated as trees of nodes. The topmost element of the tree is called the root element

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

<bookstore> ---> Root element node
  <book>
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>   ---------> element node
    <year>2005</year>
    <price>29.99</price>
  </book>

  <book>
    <title lang="en">Phoenix Project</title>
    <author>Gene Kim</author>   
    <year>2010</year>
    <price>19.99</price>
  </book>  
</bookstore> 

lang=en is attribute node.
Atomic values are nodes with no children or parent. For example J K. Rowling or "en".

EXAMPLE

<?xml version="1.0" encoding="utf-8"?>
<Wikimedia>
  <projects>
    <project name="Wikipedia" launch="2001-01-05">
      <editions>
        <edition language="English">en.wikipedia.org</edition>
        <edition language="German">de.wikipedia.org</edition>
        <edition language="French">fr.wikipedia.org</edition>
        <edition language="Polish">pl.wikipedia.org</edition>
        <edition language="Spanish">es.wikipedia.org</edition>
      </editions>
    </project>
    <project name="Wiktionary" launch="2002-12-12">
      <editions>
        <edition language="English">en.wiktionary.org</edition>
        <edition language="French">fr.wiktionary.org</edition>
        <edition language="Vietnamese">vi.wiktionary.org</edition>
        <edition language="Turkish">tr.wiktionary.org</edition>
        <edition language="Spanish">es.wiktionary.org</edition>
      </editions>
    </project>
  </projects>
</Wikimedia>

/Wikimedia/projects/project/@name selects name attributes for all projects
/Wikimedia//editions selects all editions of all projects
/Wikimedia/projects/project/editions/edition[@language='English']/text() selects addresses of all English Wikimedia projects (text of all edition elements where language attribute is equal to English)
/Wikimedia/projects/project[@name='Wikipedia']/editions/edition/text() elects addresses of all Wikipedias (text of all edition elements that exist under project element with a name attribute of Wikipedia).

Clone this wiki locally