Friday, February 18, 2011

XPass

XPass:

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 recommendation


XPass Nodes:

In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes.

Example:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>
  <book>
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>

  <book>
    <title lang="eng">Learning XML</title>
    <price>39.95</price>
  </book
>
</bookstore>



<bookstore> is root element node.
<year>2005</year> is element node.
lang="en" is attribute node.

Atomic value means node without children or parent.
This case, for example, "en", and J K. Rowling.

Items means, atomic values or nodes.

Expression:

nodename        Selects all child nodes of names name.
bookstore       Selects all child nodes of the bookstore elements.

/               Selects from root node.
/bookstore      Selects the root element bookstore.
                "/" always represents an absolute path to an element.


//              Select nodes in the document from the curresnt node that
                match the selection no matter where they are. 

//book          Select all book elements no matter where they are in the 
                document.
bookstore//book Select all book elements that are descendant of the bookstore
                element, no matter where they are. 

.               Select the current node.
..              Select the parent of the current node.


@               Select attributes
//@lang         Select all attributes that named "lang" .



Predicates:

Predicates are used to find a specific node or node that contains a specific value, and they are always embedded in the square brackets.


/bookstore/book[1]     Selects the first book element that is the child of the
                       bookstore element. 
                       [1] is the first element, but IE wrongly expects [1]. 

bookstore/book[last()] Selects the last book element that is the child of the
                       bookstore element. 


bookstore/book[last()-1] 
                       Selects the last but one book element that is the

                       child of the bookstore element.

bookstore/book[position()<3] 
                       Selects the first two book element that is the

                       child of the bookstore element.

//title[@lang]         Selects all the title elements that have an

                       attribute named "lang".  

//title[@lang='eng']   Selects all the title elements that have an 
                       attribute named "lang" with value of 'eng". 

bookstore/book[price<35.00]
                       
Selects all the book elements of the bookstore

                       element that have a price element with a value
                       greater than 35.00.

bookstore/book[price<35.00]/title
                       
Selects all the title elements of the book

                       elements of the bookstore element
                       that have a price element with a value
                       greater than 35.00.


Wildcard:
*               Matches any element node.
@*              Matches any attribute node.
node()          Matches any node of any kind.

Path Expression:

/bookstore/*    Selects all child nodes of the bookstore element.
//*             Selects all elements in the document.
//title[@*]     Selects all title elements which have any attribute.


XPath Axes:
An axis defines a node-set relative to the current node.

AxisName
ancestor            Selects all ancestors (parent, grandparent, etc.) of
                    the current node

ancestor-or-self    Selects all ancestors (parent, grandparent, etc.) of
                    the current node and the current node itself

attribute           Selects all attributes of the current node

child               Selects all children of the current node

descendant          Selects all descendants (children, grandchildren, etc.)
                    of the current node

descendant-or-self  Selects all descendants (children, grandchildren, etc.)
                    of the current node and the current node itself

following           Selects everything in the document after the closing
                    tag of the current node

following-sibling   Selects all siblings after the current node

namespace           Selects all namespace nodes of the current node

parent              Selects the parent of the current node

preceding           Selects everything in the document that is before the
                    start tag of the current node

preceding-sibling   Selects all siblings before the current node

self                Selects the current node

Location Path Expression:

A location path can be absolute or relative.
An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:

An absolute location path:/step/step/...
A relative location path:step/step/... 

Each step is evaluated against the nodes in the current node-set.

A step consists of:
an axis (defines the tree-relationship between the selected nodes and the current node)
a node-test (identifies a node within an axis)
zero or more predicates (to further refine the selected node-set)

The syntax for a location step is:axisname::nodetest[predicate]

Examples

child::book
Selects all book nodes that are children of the current node


attribute::lang Selects the lang attribute of the current node

child::* Selects all children of the current node

attribute::* Selects all attributes of the current node

child::text() Selects all text child nodes of the current node

child::node() Selects all child nodes of the current node

descendant::book Selects all book descendants of the current node

ancestor::book Selects all book ancestors of the current node

ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node

child::*/child::price Selects all price grandchildren of the current node




.

No comments:

Post a Comment