add

XML,XPATH and XQuery – part 2

XPATH 2.0 concepts

XPATH 2.0 supports a range of properties that can be applied on XML nodes. Some properties are generic which means they can be applied on all nodes whereas some properties can be applied only on a specific type of XML nodes types.

An XML node can have one or more properties : Node  –> Name, String value, Typed value, Type  annotation.

For a better understanding, lets use the below sample XML file for our XPATH demonstration ( xml sample source : service-architecture )

 


<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<catalog>
	<product description="Cardigan Sweater" product_image="cardigan.jpg">
		<catalog_item gender="Men's">
			<item_number>QWZ5671</item_number>
			<price>39.95</price>
			<size description="Medium">
				<color_swatch image="red_cardigan.jpg">Red</color_swatch>
				<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
			</size>
			<size description="Large">
				<color_swatch image="red_cardigan.jpg">Red</color_swatch>
				<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
			</size>
		</catalog_item>
		<catalog_item gender="Women's">
			<item_number>RRX9856</item_number>
			<price>42.50</price>
			<size description="Small">
				<color_swatch image="red_cardigan.jpg">Red</color_swatch>
				<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
				<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
			</size>
			<size description="Medium">
				<color_swatch image="red_cardigan.jpg">Red</color_swatch>
				<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
				<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
				<color_swatch image="black_cardigan.jpg">Black</color_swatch>
			</size>
			<size description="Large">
				<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
				<color_swatch image="black_cardigan.jpg">Black</color_swatch>
			</size>
			<size description="Extra Large">
				<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
				<color_swatch image="black_cardigan.jpg">Black</color_swatch>
			</size>
		</catalog_item>
	</product>
</catalog>

 

XPATH syntax :

XPATH notation generally follows the url format separated by forward slash for addressing parts of an XML document.

MSDN site explained puts this context so well as given below :

 

Context for XPath Expressions

 

The evaluation of an XPath expression depends on the context against which the expression operates. The context consists of the node against which the expression is evaluated and its associated environment, which includes the following:

  • The position of the context node in the document order, relative to its siblings.
  • The size of the context — that is, the number of siblings of the context node plus one.
  • Variable bindings with which references to a variable can be resolved.
  • A function library.
  • The namespace declarations in scope for the expression.

To better appreciate the concept of context, consider a tree containing nodes. Asking for all nodes named X from the root of the tree returns one set of results, while asking for those nodes from a branch in the tree returns a different set of results. Thus, the result of an expression depends upon the context against which it executes.

XPath expressions can match specific patterns at one particular context, return the results, and perform additional operations relative to the context of the returned nodes. This gives XPath expressions extraordinary flexibility in searching throughout the document tree.

 

Basic XPath Expressions

 

The following are basic types of XPath expressions. Each type is described below.

  • Current context
  • Document root
  • Root element
  • Recursive descent
  • Specific element

 

Examples

 

The following examples show some basic XPath expressions. More complex expressions are possible by combining these simple expressions together and by using the various XPath operators and special characters.

Current context
An expression prefixed with a period and forward slash (./) explicitly uses the current context as the context. For example, the following expression refers to all <catalog> elements within the current context.

./catalog

Note that this is equivalent to the following:    catalog

Document root

 

An expression prefixed with a forward slash (/) uses the root of the document tree as the context. For example, the following expression refers to the <catalog> element at the root of this document:

/catalog
Root element

 

An expression that uses a forward slash followed by an asterisk (/*) uses the root element as the context. For example, the following expression finds the root element of the document:

/*
Recursive descent
An expression that uses the double forward slash (//) indicates a search that can include zero or more levels of hierarchy. When this operator appears at the beginning of the pattern, the context is relative to the root of the document. For example, the following expression refers to all <catalog_item> elements anywhere within the current document:

//catalog_item

The .// prefix indicates that the context starts at the level in the hierarchy indicated by the current context.

Specific elements
An expression that starts with an element name refers to a query of the specific element, starting from the current context node. For example, the following expression refers to the <price> element within the <catalog_item> element in the current context node:

catalog_item/price

Some more examples to extract certain nodes from XPATH :

More details can be found in MSDN site.

 

 

Lets discuss about XQuery in section XML,XPATH, XQuery – Part 3

Written by Ramesh Metta


Leave a Reply

Your email address will not be published. Required fields are marked *

*
*