FACTOID # 60: Japan's water has a very high dissolved oxygen concentration - but not enough to prevent drowning in the bath.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RELATED ARTICLES
People who viewed "XPath" also viewed:
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > XPath

XPath (XML Path Language) is an expression language for addressing portions of an XML document, or for computing values (strings, numbers, or boolean values) based on the content of an XML document. The Extensible Markup Language (XML) is a general-purpose markup language. ...


The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. In popular use (though not in the official specification), an XPath expression is often referred to simply as an XPath.


Originally motivated by a desire to provide a common syntax and behavior model between XPointer and XSLT, XPath has rapidly been adopted by developers as a small query language. XPointer is a system for addressing components of XML based internet media. ... ... Query languages are computer languages used to make queries into databases and information systems. ...

Contents

Notation

The most common kind of XPath expression (and the one which gave the language its name) is a path expression. A path expression is written as a sequence of steps to get from one XML node (the current 'context node') to another node or set of nodes. The steps are separated by "/" (i.e. path) characters. Each step has three components:

  • Axis Specifier
  • Node Test
  • Predicate

Two notations are defined; the first, known as abbreviated syntax, is more compact and allows XPaths to be written and read easily using intuitive and, in many cases, familiar characters and constructs. The full syntax is more verbose, but allows for more options to be specified, and is more descriptive if read carefully.


Abbreviated syntax

The compact notation allows many defaults and abbreviations for common cases. The simplest XPath takes a form such as

  • /A/B/C

which selects C elements that are children of B elements that are children of the A element that forms the outermost element of the XML document. XPath syntax is designed to mimic URI (Uniform Resource Identifier) syntax and file path syntax. A Uniform Resource Identifier (URI), is a compact string of characters used to identify or name a resource. ...


More complex expressions can be constructed by specifying an axis other than the default 'child' axis, a node test other than a simple name, or predicates, which can be written in square brackets after any step. For example, the expression

  • A//B/*[1]

selects the first element ('[1]'), whatever its name ('*'), that is a child ('/') of a B element that itself is a child or other, deeper descendant ('//') of an A element that is a child of the current context node (the expression does not begin with a '/').


Expanded syntax

In the full, unabbreviated syntax, the two examples above would be written

  • /child::A/child::B/child::C
  • child::A/descendant-or-self::node()/child::B/child::*[position()=1]

Here, in each step of the XPath, the axis (e.g. child or descendant-or-self) is explicitly specified, followed by :: and then the node test, such as A or node() in the examples above.


Axis specifiers

The Axis Specifier indicates navigation direction within the tree representation of the XML document. The axes available, in the full and then the abbreviated syntax, are:

child 
default, does not need specifying in abbreviated syntax
attribute 
@
descendant 
//
descendant-or-self 
not available in abbreviated syntax
parent 
.. i.e. dot-dot
ancestor 
not available in abbreviated syntax
ancestor-or-self 
not available in abbreviated syntax
following 
not available in abbreviated syntax
preceding 
not available in abbreviated syntax
following-sibling 
not available in abbreviated syntax
preceding-sibling 
not available in abbreviated syntax
self 
. i.e. dot
namespace 
not available in abbreviated syntax

As an example of using the attribute axis in abbreviated syntax, //a/@href selects an attribute called href in an a element anywhere in the document tree. The self axis is most commonly used within a predicate to refer to the currently selected node. For example, h3[.='See also'] selects an element called h3 in the current context, whose text content is See also.


Node tests

Node tests may consist of specific node names or more general expressions. In the case of an XML document in which the namespace prefix gs has been defined, //gs:enquiry will find all the enquiry elements in that namespace, and //gs:* will find all elements, regardless of local name, in that namespace.


Other node test formats are:

comment() 
finds an XML comment node, e.g. <!-- Comment -->
text() 
finds a node of type text, e.g. the hello in <k>hello</k>
processing-instruction() 
finds XML processing instructions such as <?php echo $a; ?>. In this case, processing-instruction('php') would match.
node() 
finds any node at all.

Predicates

Expressions of any complexity can be specified in square brackets, that must be satisfied before the preceding node will be matched by an XPath. For example //a[@href='help.php'], which will match an a element with an href attribute whose value is help.php.


There is no limit to the number of predicates in a step, and they need not be confined to the last step in an XPath. They can also be nested to any depth. Paths specified in predicates begin at the context of the current step (i.e. that of the immediately preceding node test) and do not alter that context. All predicates must be satisfied for a match to occur.


When //a[/html/@lang='en'][@href='help.php'][1]/@target is applied to a XHTML document, it selects the value of the target attribute of the first a element that has its href attribute set to help.php, provided the document's html top-level element also has a lang attribute set to en. The reference to an attribute of the top-level element in the first predicate affects neither the context of other predicates nor that of the location step itself. The Extensible HyperText Markup Language, or XHTML, is a markup language that has the same depth of expression as HTML, but also conforms to XML syntax. ...


Predicate order is significant, however. Each predicate 'filters' a location step's selected node-set in turn. //a[1][/html/@lang='en'][@href='help.php']/@target will find a match only if the first a element in a @lang='en' document also meets @href='help.php'


Functions and operators

XPath 1.0 defines four data types: node-sets (sets of nodes with no intrinsic order), strings, numbers and booleans.


The available operators are:

  • The "/", "//" and "[...]" operators, used in path expressions, as described above.
  • A union operator, "|", which forms the union of two node-sets.
  • Boolean operators "and" and "or", and a function "not()"
  • Arithmetic operators "+", "-", "*", "div" (divide), and "mod"
  • Comparison operators "=", "!=", "<", ">", "<=", ">="

The function library includes:

  • Functions to manipulate strings: concat(), substring(), contains(), substring-before(), substring-after(), translate(), normalize-space(), string-length()
  • Functions to manipulate numbers: sum(), round(), floor(), ceiling()
  • Functions to get properties of nodes: name(), local-name(), namespace-uri()
  • Functions to get information about the processing context: position(), last()
  • Type conversion functions: string(), number(), boolean()

Some of the more commonly useful functions are detailed below. For a complete description, see the W3C Recommendation document


Node set functions

position() 
returns a number representing the position of this node in the sequence of nodes currently being processed (for example, the nodes selected by an xsl:for-each instruction in XSLT).
count(node-set
returns the number of nodes in the node-set supplied as its argument.

String functions

string(object?) 
converts any of the four XPath data types into a string according to built-in rules. If the value of the argument is a node-set, the function returns the string-value of the first node in document order, ignoring any further nodes.
concat(string, string, string*) 
concatenates any number of strings
contains(s1, s2
returns true if s1 contains s2
normalize-space(string?) 
all leading and trailing whitespace is removed and any sequences of whitespace characters are replaced by a single space. This is very useful when the original XML may have been prettyprint formatted, which could make further string processing unreliable.

For information on the programming language, see Whitespace programming language. ... To prettyprint (or pretty-print) is to present an object to a human reader, so that it is easier to perceive the objects structure, or, less commonly, to simply make it more attractive. ...

Boolean functions

not(boolean
negates any boolean expression.
true() 
evaluates to true.
false() 
evaluates to false.

Number functions

sum(node-set
converts the string values of all the nodes found by the XPath argument into numbers, according to the built-in casting rules, then returns the sum of these numbers.

Expressions can be created inside predicates using the operators: =, !=, <=, <, >= and >. Boolean expressions may be combined with brackets () and the boolean operators and and or as well as the not() function described above. Numeric calculations can use *, +, -, div and mod. Strings can consist of any Unicode characters. Unicode is an industry standard designed to allow text and symbols from all of the writing systems of the world to be consistently represented and manipulated by computers. ...


Inside or outside of predicates, entire node-sets can be combined ('unioned') using the pipe character |. In set theory and other branches of mathematics, the union of a collection of sets is the set that contains everything that belongs to any of the sets, but nothing else. ...


v[x or y] | w[z] will return a single node-set consisting of all the v elements that have x or y child-elements, as well as all the w elements that have z child-elements, that were found in the current context.


//item[@price > 2*@discount] selects items whose price attribute is at least twice the numeric value of the discount attribute.


XPath 2.0

Main article: XPath 2.0

XPath 1.0 was published as a W3C Recommendation on November 16, 1999; XPath 2.0 was published as a W3C Recommendation on January 23, 2007. XPath 2.0 represents a significant increase in the size and capability of the XPath language. XPath 2. ... A W3C Recommendation is the final stage of a ratification process of the W3C working group concerning the standard. ... November 16 is the 320th day of the year (321st in leap years) in the Gregorian calendar, with 45 days remaining. ... Year 1999 (MCMXCIX) was a common year starting on Friday (link will display full 1999 Gregorian calendar). ... XPath 2. ... January 23 is the 23rd day of the year in the Gregorian calendar. ... 2007 (MMVII) is the current year, a common year starting on Monday of the Gregorian calendar and the AD/CE era. ...


The most notable change is that XPath 2.0 has a much richer type system; XPath 2.0 supports atomic types, defined as built-in types in XML Schema, and may also import user-defined types from a schema. Every value is now a sequence (a single atomic value or node is regarded as a sequence of length one). XPath 1.0 node-sets are replaced by node sequences, which may be in any order. An XML schema is a description of a type of XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic syntax constraints imposed by XML itself. ...


To support richer type sets, XPath 2.0 offers a greatly expanded set of functions and operators.


XPath 2.0 is in fact a subset of XQuery 1.0. It offers a for expression which is cut-down version of the "FLWOR" expressions in XQuery. It is possible to describe the language by listing the parts of XQuery that it leaves out: the main examples are the query prolog, element and attribute constructors, the remainder of the "FLWOR" syntax, and the typeswitch expression. XQuery is a query language (with some programming language features) that is designed to query collections of XML data. ... The programming language XQuery defines FLWOR (or FLWR) as expression that supports iteration and binding of variables to intermediate results. ...


Implementations

Actionscript
  • XPath4AS/XPath4AS2 [1]
C/C++
Implementations for Database Engines
Java
  • Jaxen is an Open Source XPath implementation supporting (embedded by) multiple XML parsers (XOM, Dom4J, JDom).
  • Apache Xalan-Java supports XPath 1.0 (as well as XSLT 1.0)
  • Saxon supports XPath 1.0 and XPath 2.0 (as well as XSLT 1.0, XSLT 2.0, and XQuery 1.0)
  • VTD-XML [2]

The Java package javax.xml.xpath has been part of Java standard edition since Java 5. Technically this is an XPath API rather than an XPath implementation, and it allows the programmer the ability to select a specific implementation that conforms to the interface. The correct title of this article is . ... VTD-XML (http://vtd-xml. ... The current version of the article or section reads like an advertisement. ... VTD-XML (http://vtd-xml. ... Java is a programming language originally developed by Sun Microsystems and released in 1995. ... API may refer to: In computing, application programming interface In petroleum industry, American Petroleum Institute In education, Academic Performance Index This page concerning a three-letter acronym or abbreviation is a disambiguation page — a navigational aid which lists other pages that might otherwise share the same title. ...

.NET
  • In the System.Xml and System.Xml.Xpath namespaces [3]
  • VTD-XML [4]
Perl
PHP
Python
  • PyXML
  • libxml2
  • ElementTree
  • 4Suite
  • Amara
Ruby

VTD-XML (http://vtd-xml. ...

Use of XPath in Schema Languages

XPath is increasingly used to express constraints in schema languages for XML.

  • The (now ISO standard) schema language Schematron pioneered the approach.
  • A streaming subset of XPath is used in W3C XML Schema for expressing uniqueness and key constraints.
  • XForms uses XPath to bind types to values.
  • The approach has even found use in non-XML applications, such as the constraint language for Java called PMD: the Java is converted to a DOM-like parse tree, then XPaths rules are defined over the tree.

This article or section does not cite any references or sources. ... The Schematron is an XML structure validation language for making assertion about the presence or absence of patterns in trees. ... An XML schema is a description of a type of XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic syntax constraints imposed by XML itself. ... XForms is two things: 1. ...

See also

The Extensible Markup Language (XML) is a general-purpose markup language. ... The eXtensible Stylesheet Language (XSL) is a family of transformation languages which allows one to describe how files encoded in the XML standard are to be formatted or transformed. ... ... XSL Formatting Objects, or XSL-FO, is an XML markup language for document formatting. ... XQuery is a query language (with some programming language features) that is designed to query collections of XML data. ... The XML Linking Language, or XLink, is an XML markup language used for creating hyperlinks within XML documents. ... XPointer is a system for addressing components of XML based internet media. ... An XML schema is a description of a type of XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic syntax constraints imposed by XML itself. ... The Schematron is an XML structure validation language for making assertion about the presence or absence of patterns in trees. ... Navigational databases incorporate both the network model and hierarchical model of database interfaces. ... The eXtensible Stylesheet Language (XSL) is a family of transformation languages which allows one to describe how files encoded in the XML standard are to be formatted or transformed. ... Diagram of the basic elements and process flow of Extensible Stylesheet Language Transformations. ... XSL Formatting Objects, or XSL-FO, is an XML markup language for document formatting which is most often used to generate PDFs. ...

External links


  Results from FactBites:
 
XPath - Wikipedia, the free encyclopedia (1185 words)
XPath (XML Path Language) is an expression language for addressing portions of an XML document, or for computing values (strings, numbers, or boolean values) based on the content of an XML document.
The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria.
The most notable change is that XPath 2.0 has a much richer type system; XPath 2.0 supports atomic types, defined as built-in types in XML Schema, and may also import user-defined types from a schema.
XML - Understanding XPath Tutorials (888 words)
XPath is a technology that enables you to address parts of an XML document, such as a specific element or set of elements.
XPath's departure from XML syntax also makes it both flexible and compact, which are important benefits when you consider that XPath is typically used in constrained situations such as attribute values.
XPath is based upon the notion of an XML document consisting of a hierarchical tree of nodes.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.