Introduction to XPath:
Full form of XPath is XML Path. It is a query language designed to traverse through an xml document and select the required nodes using XPath Expressions and XPath functions, which I will discuss in the next chapters. XPath is a World Wide Web consortium (w3c) recommendation and the latest specification is Xpath 2.0.
This specification is designed to be referenced normatively from other specifications defining a host language for it. It is not intended to be implemented outside a host language. The implementation ability of this specification has been tested in the context of its normative inclusion in host languages defined by the XQuery and XSLT.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <dev:Corporation xmlns:dev="http://developprojects.com"> <dev:Organization> <dev:website category="technical"> <dev:name>developprojects.com</dev:name> <dev:topic>XPath Tutorial</dev:topic> <dev:author>Viswa Tej Swarup Reddy</dev:author> <dev:price>FREE</dev:price> </dev:website> </dev:Organization> </dev:Corporation>
Regular XPath terminology:
The common XPath vocabularies you must know before proceeding further are:
- Atomic Values
Every XML document is a tree of nodes. Various types of nodes are
- Tag nodes
- Element nodes
- Attribute nodes
- Text nodes
In the above XML example,
< Corporation> is the root node.
<Organization> tag is a node.
<topic> XPath Tutorial</topic> is an element node.
category=”technical” is an attribute node.
XPath Tutorial is a text node.
And in turn these nodes can be categorized internally based on their relationships in between.
Some of them are namely
- Ancestor: Ancestor nodes are parent nodes, if the parent nodes have parents, then we have to include their names too and it follows till the root node. In the above example the ancestor nodes of <author> node are <website>,<Organization>, <Corporation>.
- Descendent: Descendent nodes are Child nodes, if the child nodes have children, then we have to include their names too and if follows till the lowest node is reached. In the above example the descendent nodes of <Organization> node are <website>, <name>, <topic>, <author>, <price>.
- Parent: Parent nodes are immediate parent of the selected node or attribute. In the above example <website> is the parent node for <name>, <topic>, <author>, <price>.
- Child: Child nodes are the immediate child nodes of the selected nodes. In the above example, child nodes for <website> are <name>, <topic>, <author>, <price>.
- Siblings: The nodes which share a common parent are called siblings. In the above example, <name>, <topic>, <author>, <price> are siblings who share a common parent ,<website>.
- Text: Text nodes are just the values of the individual nodes. In the above example, developprojects.com, XPath Tutorial, Viswa Tej Swarup Reddy, FREE are all text nodes.
It may sound redundant but we can also categorize node types based on the node’s functionality and purpose. They are:
- Document Node or Root Node
- Processing Instruction
- Document Node or Root Node: The top most element of an XML document is document node. All the other elements remain within the document node.In the Above example <Corporation> is the document node.
- Elements: Elements are contained within the document node. Anything in an XML document with Opening and closing tag is called an Element.
- Attributes: Attributes describe the element and usually lies in the opening tag. Example: category=”technical” describes that the website element belong to the technical category. In case if we have two elements with same name, we can differentiate them using attribute node.
- Comments: Comments are text defined in XML document for describing things to other users. They are contained in between the <! — and à tags.
- Namespace: Different systems use different tag names for their nodes. There is always a possibility of name conflict. So, a better option is to use prefix before every tag name. This prefix will have to be defined using xmlns attribute. The syntax would be like <element xmlns:prefix=”URI”>.
- Processing Instruction: Processing Instruction is the most import element of an XML document in real time scenarios. It instructs the renderer which may be a browser or another application which uses this xml document, the encoding format and whether it is standalone or not. In our example, we have used standard web encoding=”UTF-8″ tells us that the encoding is a 8 bit Unicode format and standalone=”yes” tell us that the xml document is self-contained . The standalone declaration means that the document is self-contained. That in turn means one of three things:
- There is no DOCTYPE declaration in it.
- The DOCTYPE declaration is inline only.
- The DOCTYPE declaration is external or combined, but the external part contains no data that changes the infoset representation of the document.
- Text: Text nodes are text defined in Element or Attribute or Processing instruction.Examples of text nodes are XPath Tutorial, technical.
Properties of Nodes:
The following are the various types of Properties that a node can have:
- Name: Name Property can be applied to Elements, attributes, namespaces, processing instructions and document node. A Name may contain a combination local name and prefix. We can extract those values using the standard functions. Namespace-uri() function for accessing the prefix and local-name() function for local name. Example: dev is prefix and Corporation is local name.
- String values: It refers to the values of the nodes. String() function helps us for getting the value of the node.
- Base URI
- Type Annotation
Atomic values: Atomic values are nodes with no parent or child nodes. They are indeed similar to text nodes. developprojects.com, XPath Tutorial, Viswa Tej Swarup Reddy, FREE are all atomic values.
Items:Everything in XPath is indeed a sequence of items. Items can be either nodes or atomic values. It is just similar to a node which may or may not contain parent or child nodes.