Monday, January 19, 2015

2015: Intw question 3- Basic Xpath

XPath
  • XPath is used to navigate through elements and attributes in an XML document.
  • XPath is a language for finding information in an XML document.
  • In XPath, there are seven kinds of nodes: 
    • element
    • attribute
    • text
    • namespace
    • processing-instruction
    • comment
    • document
  • Look at the following XML document:
<?xml version="1.0" encoding="UTF-8"?>

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

Example of nodes in the XML document above:
<bookstore> (root element node)

<author>J K. Rowling</author> (element node)

lang="en" (attribute node)

  • Atomic values/Items/NodeAtomic values are nodes with no children or parent. e.g. 
J K. Rowling

"en"

  • Relationship of Nodes
Refer following example
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

    • Parent- Each element and attribute has one parent. e.g. book is parent is of title/author/year/price
    • Children- Element nodes may have zero, one or more children. e.g. title/author/year/price are children of book
    • Siblings- Nodes that have same parent e.g. title/author/year/price
    • Ancestors- Node's parent, parent's parent e.g. book is ancestor of title/author/year/price
    • Descendants- Node's children, children's children e.g. title/author/year/price are descendants of book

  • Basic XPath Syntax- Refer following article -
       http://www.w3schools.com/xpath/xpath_syntax.asp

No comments:

Post a Comment