18
XML QUERY LANGUAGE AND NAVIGATION

Xml query language and navigation

Embed Size (px)

Citation preview

Page 1: Xml query language and navigation

XML QUERY LANGUAGE AND NAVIGATION

Page 2: Xml query language and navigation

XML sprang to life as a metalanguage that can be used to describe any sort of data and

documents using a truly hierarchical representation, or a representation that simply looks

hierarchical.

In the Microsoft .NET Framework, XPath is fully supported through the classes defined in the System.Xml.XPath namespace

What Is Xpath ?

XPath is a general-purpose query language for addressing and filtering both the elements and

the text of an XML document. As the name suggests, the XPath notation is basically

declarative. A valid XPath expression looks like a path to a particular set of nodes or a value

excerpted from the source document.

Page 3: Xml query language and navigation

XPath works on top of a tree-based representation of the source document.

The path expresses a node pattern using a notation that emphasizes the hierarchical

relationship between the nodes.

Page 4: Xml query language and navigation

This expression states: find all the address nodes that happen to be children of the customer

element. But on which nodes is this expression evaluated? An Xpath expression is always

evaluated in the context of a node. The context node is designated by the application and

represents the starting point of the query. Expressing the concept of the context node in terms

of file system paths, we could say that the appropriate file system counterpart for the context

node is the current directory. The nodes affected by the expression form the context node-set.

The final set of nodes that is actually returned to the application is a subset of the context

node-set that includes only those nodes that match the specified criteria.

Page 5: Xml query language and navigation

Context of XPath Queries

The context of an XPath query includes, but is not limited to, a context node and a context node-set. The node-set. The XPath context also contains position and namespace information, variable bindings, and a bindings, and a standard library of functions. We'll look at the contents of the XPath context in detail in detail in this section.

In the .NET Framework, the context node is the XmlNode object on which you call either the SelectNodes

Page 6: Xml query language and navigation

A SAMPLE XML TREE IN WHICH THE NODE NUMBERS INDICATE THE ORDER IN WHICH NODES ARE VISITED BY THE

XPATH QUERY PROCESSOR.

Page 7: Xml query language and navigation

Position Information

An XPath context is characterized by a position and a size. The position attribute is a one-based value

Page 8: Xml query language and navigation

XPath and Namespaces

The XPath processor uses node information to determine whether a match exists with the current current expression. The most important information used by XPath expressions is the node's name, type, name, type, and attributes. XPath fully supports XML namespaces and splits the name of a node into two node into two constituent parts: the namespace URI and the local name. The set of namespaces declared namespaces declared in scope for the context node is used to qualify node names in the expression.expression.

Variable Bindings

An XPath expression can contain variable references that are resolved through a set of in-memory memory bindings established between variable names and actual values. Each variable holds a value a value whose type is normally one of the four base types—node-set, string, Boolean, and number. It is

Page 9: Xml query language and navigation

WHAT IS XPOINTER?

XPointer is used to locate data within an XML document. When XML documents need to point

to external resources, they can declare an entity reference or, more effectively, include the

whole resource, using the XML Inclusion (XInclude) syntax. XInclude—a W3C recommendation

candidate—links the host document to an external resource, or a portion of it. XPointer defines

the syntax you use to specify the addressed portion of the document.

Normally, to indicate a particular position in an XML document, you attach a fragment

identifier to the document's URL. A fragment identifier is marked by a number sign (#) and

follows the document's URL. For example, the URL http://www.w3.org/TR/xptr/#conformance

points to the portion of the document labeled with the conformance name. With XPointer, you

can use the XPath syntax to identify with greater flexibility a particular location in the external

document

Page 10: Xml query language and navigation

How XPointer Uses Xpath

An XPointer fragment identifier can be the name of a particular portion of the target document, but it

Page 11: Xml query language and navigation

XPATH IN THE XML DOM

In the .NET Framework, you can make use of XPath expressions in two ways: through the XML DOM or by means of a new and more flexible API based on the concept of the XPath navigator.

In the former case, you use XPath expressions to select nodes within the context of a living instance of the XmlDocument class.

Page 12: Xml query language and navigation

THE XML DOM NODE RETRIEVAL API

When using XPath queries to query an XML DOM instance, you can use the SelectNodes method of the XmlDocument class. In particular, SelectNodes returns a collection that contains instances of all the XmlNode objects that match the specified expression. If you don't need the entire node-set, but instead plan to use the query to locate the root of a particular subtree, use the SelectSingleNode method.

SelectSingleNode takes an XPath expression and returns a reference to the first match found.

Page 13: Xml query language and navigation
Page 14: Xml query language and navigation

The XPathNavigator Class

The programming interface of the navigator object is defined in the XPathNavigator abstract class. The class. The XPathNavigator class represents a generic interface designed to act as a reader for any data any data that exposes its contents as XML.

Functionally speaking, the XPathNavigator class is not much different from a pseudoclass that simply simply groups together all the XML DOM methods (ChildNodes, SelectNodes, and SelectSingleNode) to

Page 15: Xml query language and navigation

XPATH NAVIGATORS AND XML READERS

The MSDN documentation defines an XPath navigator as a class that reads data from an XML-based data store using a cursor model. XPathNavigator, therefore, provides read-only, random access to the underlying XML-based data.

Page 16: Xml query language and navigation

XPath navigators and XML readers are radically different objects, although both look like client-

side cursors for reading XML data. Let's review the key differences:

Connection model

Navigation interface

Programming interface

Page 17: Xml query language and navigation

THE RELATIONSHIP BETWEEN CALLERS, NAVIGATORS, AND

ITERATORS.

Page 18: Xml query language and navigation

On the long road to standardization, XPath seems like the first significant step toward a

universal query language to keep up with the universal protocol (HTTP), the universal data

description language (XML), and the universal remote procedure call protocol (SOAP).