This post is a small introduction for xpath (XML Path Language) is a terse (non-XML) syntax for addressing portions of an XML document. XPath has rapidly been adopted by developers as a small query language.
With the increase in usage of xml being sent on internet for transferring data there was huge need of some standard way for accessing data in a simple and fast manner.Thats what is Xpath is about.
Introduction to Xpath
XPath is a language for finding information in an XML document. It allows consumers of Xml to query it and access any part of it directly.It may return null, a string, a number or an xml node itself. XPath is used to navigate through elements and attributes in an XML document.
What is XPath?
- XPath is a syntax for defining parts of an XML document.
- XPath uses path expressions to navigate in XML documents .
- XPath contains a library of standard functions.
- XPath is a major element in XSLT.
- XPath is a W3C Standard.
Now when it says expression we are not talking abt scary regular expressions but simple expressions which we use it for our desktop searching often like [.*].Now lets start with an example and go ahead in this.
Example XML File.
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<bookstore>[document node]<
book><title lang=”en”>[attribute]Harry Potter</title>[element]<
author>JKRowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
An XML is treated as a tree of nodes.If we closely look there are around seven kind of nodes defined by XPath.The root of this tree is called as document node or root node.The nodes are element, attribute, text, namespace, processing-instruction, comment, and document (root)nodes.
XML NODES
- bookstore is root node,title is an element and lang=’en’ is attribute.
- ParentEach element and attribute has one parent.In the example; the book element is the parent of the title, author, year, and price.
- Children:Element nodes may have zero, one or more children.In the example; the title, author, year, and price elements are all children of the book element.
- Siblings : Nodes that have the same parent.In the example; the title, author, year, and price elements are all siblings.
- Ancestors : A node’s parent, parent’s parent, etc.In the example; the ancestors of the title element are the book element and the bookstore element.
- Descendants: A node’s children, children’s children, etc. In the example; descendants of the bookstore element are the book, title, author, year, and price elements.
Now since we are clear with the basics of xpath we can take an example of it and work along.Let’s try to learn some basic XPath syntax by looking at some examples.
The XML Example Document
<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book><book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book><book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book><book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore>
Selecting Nodes:
We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:
set xmlDoc=CreateObject(”Microsoft.XMLDOM”)
xmlDoc.async=”false”
xmlDoc.load(”books.xml”)xmlDoc.selectNodes(path expression)
Select all book Nodes:
The following example selects all the book nodes under the bookstore element:
xmlDoc.selectNodes(”/bookstore/book”)
Select the First book Node:
The following example selects only the first book node under the bookstore element:
xmlDoc.selectNodes(”/bookstore/book[0]“)
Select the prices:
The following example selects the text from all the price nodes:
xmlDoc.selectNodes(”/bookstore/book/price/text()”)
Selecting price Nodes with Price>35:
The following example selects all the price nodes with a price higher than 35:
xmlDoc.selectNodes(”/bookstore/book[price>35]/price”)
Thus from the above example we can see how to navigate through an xml document and navigate through it and retrieve the data.This was just a small idea how it works.XPath is bigger than this.If you have still some query i would refer to go through more books to see the actual implementation of it in detail.
For a Complete details i would like to recommend to go through standards and follow the links
External links:
Thank you very much, very informative and helpful.
Nothing informative. Every thing is copied and pasted. Please give some informative, extra and easy understanding to the users.