What is XPath?

The Essence of LINQ

 

This excerpt from Processing XML documents with Oracle JDeveloper 11g by Deepak Vohra, is printed with permission from Packt Publishing , Copyright 2007. 


What is XPath?

XPath is a language for addressing an XML document's elements and attributes. As an example, say you receive an XML document that contains the details of a shipment and you want to retrieve the element/attribute values from the XML document. You don't just want to list the values of all the nodes, but also want to output the values of specifi c elements or attributes. In such a case, you would use XPath to retrieve the values of those elements and attributes. XPath constructs a hierarchical structure of an XML document, a tree of nodes, which is the XPath data model. The XPath data model consists of seven node types. The different types of nodes in the XPath data model are discussed in the following table:

Node Type Description
Root Node The root node is the root of the DOM tree. The document element (the root element) is a child of the root node. The root node also has the processing instructions and comments as child nodes.
Element Node This represents an element in an XML document. The character data, elements, processing instructions, and comments within an element are the child nodes of the element node.
Attribute Node This represents an attribute other than the xmlns-prefi xed attribute, which declares a namespace.
Text Node The character data within an element is a text node. A text node has at least one character of data. A whitespace is also considered as a character of data. By default, the ignorable whitespace after the end of an element and before the start of the following element is also a text node. The ignorable whitespace can be excluded from the DOM tree built by parsing an XML document. This can be done by setting the whitespace-preserving mode to false with the setPreserveWhitespace(boolean flag) method.
Comment Node This represents a comment in an XML document, except the comments within the DOCTYPE declaration.
Processing Instruction Node This represents a processing instruction in an XML document except the processing instruction within the DOCTYPE declaration. The XML declaration is not considered as a processing instruction node.
Namespace Node This represents a namespace mapping, which consists of a xmlns:-prefi xed attribute such as xmlns:xsd="http://www. w3.org/2001/XMLSchema". A namespace node consists of a namespace prefi x (xsd in the example) and a namespace URI (http://www.w3.org/2001/XMLSchema in the example).

Specifi c nodes including element, attribute, and text nodes may be accessed with XPath. XPath supports nodes in a namespace. Nodes in XPath are selected with an XPath expression. An expression is evaluated to yield an object of one of the following four types: node set, Boolean, number, or string.

An example of a location step is:

child::journal[position()=2]

In the example, the child axis contains the child nodes of the context node. Node test is the journal node set, and predicate is the second node in the journal node set. An absolute location path is defi ned with respect to the root node, and starts with "/". The difference between a relative location path and an absolute location path is that a relative location path starts with a location step, and an absolute location path starts with "/". 


Next>>

Also read

Querying and modifying XML data in SQL Server 2005

What is XQuery language?
Explain the syntax rule of XQuery language.
XQuery expression contains two parts: the Prolog and the Body. Explain them
Explain PATH expression in XQuery with an example.

What is XML data binding?

Answer - XML data binding refers to the process of representing the information in an XML document as an object in computer memory...............  

How do you load data from XML file to a ORACLE table?

Answer - You need to first create a table in oracle that matches with the fields of the XML data...............  

What is the purpose of FOR XML in SQL Server?

Answer - SQL Server 2000 provides the facility to retrieve data in the form of XML with the help of the FOR XML clause appended to the end of a SELECT statement. ..............



Write your comment - Share Knowledge and Experience

Discussion Board
Xpath Attributes

Hi ,
I want to know the meaning of below symbols used in xpath -
., @, /, [ ] etc

Thanks in advance


Prasshant Rane 11-8-2013 09:50 AM

 

 

 
Latest placement tests
Latest links
 
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring