Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

XPath

From Wikipedia, the free encyclopedia
(Redirected fromXPath 1.0)
Expression language for XML documents
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "XPath" – news ·newspapers ·books ·scholar ·JSTOR
(August 2010) (Learn how and when to remove this message)
XPath
ParadigmQuery language
DeveloperW3C
First appeared1998
Stable release
3.1 / March 21, 2017; 8 years ago (2017-03-21)
Influenced by
XSLT,XPointer
Influenced
XML Schema,XForms,JSONPath

XPath (XML Path Language) is anexpression language designed to support the query or transformation ofXML documents. It was defined by theWorld Wide Web Consortium (W3C) in 1999,[1] and can be used to compute values (e.g.,strings, numbers, orBoolean values) from the content of an XML document. Support for XPath exists in applications that support XML, such as web browsers, and many programming languages.

The XPath language is based on atree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria.[2][3] 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 betweenXPointer andXSLT, subsets of the XPathquery language are used in otherW3C specifications such asXML Schema,XForms and theInternationalization Tag Set (ITS).

XPath has been adopted by a number of XML processing libraries and tools, many of which also offerCSS Selectors, another W3C standard, as a simpler alternative to XPath.

Versions

[edit]

There are several versions of XPath in use. XPath 1.0 was published in 1999, XPath 2.0 in 2007 (with a second edition in 2010), XPath 3.0 in 2014, and XPath 3.1 in 2017. However, XPath 1.0 is still the version that is most widely available.[1]

  • XPath 1.0 became a Recommendation on 16 November 1999 and is widely implemented and used, either on its own (called via an API from languages such asJava,C#,Python orJavaScript), or embedded in languages such asXSLT,XProc,XML Schema orXForms.
  • XPath 2.0 became a Recommendation on 23 January 2007, with a second edition published on 14 December 2010. A number of implementations exist but are not as widely used as XPath 1.0. The XPath 2.0 language specification is much larger than XPath 1.0 and changes some of the fundamental concepts of the language such as the type system.
    The most notable change is that XPath 2.0 is built around theXQuery and XPath Data Model (XDM) that has a much richer type system.[a] 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.
    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 ofXQuery 1.0. They share the same data model (XDM). It offers afor expression that is a 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 thetypeswitch expression.
  • XPath 3.0 became a Recommendation on 8 April 2014.[4] The most significant new feature is support for functions as first-class values.[5] XPath 3.0 is a subset of XQuery 3.0, and most current implementations (April 2014) exist as part of an XQuery 3.0 engine.
  • XPath 3.1 became a Recommendation on 21 March 2017.[6] This version adds new data types: maps and arrays, largely to underpin support forJSON.
XPath expression applied to an XML file

Syntax and semantics (XPath 1.0)

[edit]

The most important kind of expression in XPath is alocation path. A location path consists of a sequence oflocation steps. Each location step has three components:

An XPath expression is evaluated with respect to acontext node. An Axis Specifier such as 'child' or 'descendant' specifies the direction to navigate from the context node. The node test and the predicate are used to filter the nodes specified by the axis specifier: For example, the node test 'A' requires that all nodes navigated to must have label 'A'. A predicate can be used to specify that the selected nodes have certain properties, which are specified by XPath expressions themselves.

The XPath syntax comes in two flavors: theabbreviated syntax, is more compact and allows XPaths to be written and read easily using intuitive and, in many cases, familiar characters and constructs. Thefull syntax is more verbose, but allows for more options to be specified, and is more descriptive if read carefully.

Abbreviated syntax

[edit]

The compact notation allows many defaults and abbreviations for common cases. Given source XML containing at least

<A><B><C/></B></A>

the simplest XPath takes a form such as

  • /A/B/C

that 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. The XPath syntax is designed to mimic URI (Uniform Resource Identifier) andUnix-style file path syntax.

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 child ('*[1]'), whatever its name, of every 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 '/'). The predicate[1] binds more tightly than the/ operator. To select the first node selected by the expressionA//B/*, write(A//B/*)[1]. Note also, index values in XPath predicates (technically, 'proximity positions' of XPath node sets) start from 1, not 0 as common in languages like C and Java.

Expanded syntax

[edit]

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::node()[position()=1]

Here, in each step of the XPath, theaxis (e.g.child ordescendant-or-self) is explicitly specified, followed by:: and then thenode test, such asA ornode() in the examples above.

Here the same, but shorter:A//B/*[position()=1]

Axis specifiers

[edit]

Axis specifiers indicate navigation direction within the tree representation of the XML document. The axes available are:[b]

Axis specifiers in XPath
Full syntaxAbbreviated syntaxNotes
ancestor
ancestor-or-self
attribute@@abc is short forattribute::abc
childxyz is short forchild::xyz
descendant
descendant-or-self//// is short for/descendant-or-self::node()/
following
following-sibling
namespace
parent.... is short forparent::node()
preceding
preceding-sibling
self.. is short forself::node()

As an example of using theattribute axis in abbreviated syntax,//a/@href selects the attribute calledhref ina elements anywhere in the document tree.The expression. (an abbreviation forself::node()) is most commonly used within a predicate to refer to the currently selected node.For example,h3[.='See also'] selects an element calledh3 in the current context, whose text content isSee also.

Node tests

[edit]

Node tests may consist of specific node names or more general expressions. In the case of an XML document in which the namespace prefixgs has been defined,//gs:enquiry will find all theenquiry 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 excluding any children, e.g. thehello in<k>hello<m> world</m></k>
processing-instruction()
finds XMLprocessing instructions such as<?phpecho$a;?>. In this case,processing-instruction('php') would match.
node()
finds any node at all.

Predicates

[edit]

Predicates, written as expressions in square brackets, can be used tofilter a node-set according to some condition. For example,a returns a node-set (all thea elements which are children of the context node), anda[@href='help.php'] keeps only those elements having anhref attribute with the valuehelp.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 the value of the predicate is numeric, it is syntactic-sugar for comparing against the node's position in the node-set (as given by the functionposition()). Sop[1] is shorthand forp[position()=1] and selects the firstp element child, whilep[last()] is shorthand forp[position()=last()] and selects the lastp child of the context node.

In other cases, the value of the predicate is automatically converted to a Boolean. When the predicate evaluates to a node-set, the result is true when the node-set isnon-empty[clarify]. Thusp[@x] selects thosep elements that have an attribute namedx.

A more complex example: the expressiona[/html/@lang='en'][@href='help.php'][1]/@target selects the value of thetarget attribute of the firsta element among the children of the context node that has itshref attribute set tohelp.php, provided the document'shtml top-level element also has alang attribute set toen. 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.

Predicate order is significant if predicates test the position of a node. Each predicate takes a node-set returns a (potentially) smaller node-set. Soa[1][@href='help.php'] will find a match only if the firsta child of the context node satisfies the condition@href='help.php', whilea[@href='help.php'][1] will find the firsta child that satisfies this condition.

Functions and operators

[edit]

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 operatorsand andor, and a functionnot()
  • Arithmetic operators+,-,*,div (divide), andmod
  • 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.[c]

Node set functions

[edit]
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

[edit]
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 two or more strings
starts-with(s1,s2)
returnstrue ifs1 starts withs2
contains(s1,s2)
returnstrue ifs1 containss2
substring(string,start,length?)
example:substring("ABCDEF",2,3) returnsBCD.
substring-before(s1,s2)
example:substring-before("1999/04/01","/") returns1999
substring-after(s1,s2)
example:substring-after("1999/04/01","/") returns04/01
string-length(string?)
returns number of characters in string
normalize-space(string?)
all leading and trailingwhitespace is removed and any sequences of whitespace characters are replaced by a single space. This is very useful when the original XML may have beenprettyprint formatted, which could make further string processing unreliable.

Boolean functions

[edit]
not(boolean)
negates any Boolean expression.
true()
evaluates totrue.
false()
evaluates tofalse.

Number functions

[edit]
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.

Usage examples

[edit]

Expressions can be created inside predicates using the operators:=, !=, <=, <, >= and>. Boolean expressions may be combined with brackets() and the Boolean operatorsand andor as well as thenot() function described above. Numeric calculations can use*, +, -, div andmod. Strings can consist of anyUnicode characters.

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

Entire node-sets can be combined ('unioned') using the vertical bar character |. Node sets that meet one or more of several conditions can be found by combining the conditions inside a predicate with 'or'.

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

Syntax and semantics (XPath 2.0)

[edit]
Main article:XPath 2.0

Syntax and semantics (XPath 3)

[edit]
Main article:XPath 3

Examples

[edit]

Given a sample XML document

<?xml version="1.0" encoding="utf-8"?><Wikimedia><projects><projectname="Wikipedia"launch="2001-01-05"><editions><editionlanguage="English">en.wikipedia.org</edition><editionlanguage="German">de.wikipedia.org</edition><editionlanguage="French">fr.wikipedia.org</edition><editionlanguage="Polish">pl.wikipedia.org</edition><editionlanguage="Spanish">es.wikipedia.org</edition></editions></project><projectname="Wiktionary"launch="2002-12-12"><editions><editionlanguage="English">en.wiktionary.org</edition><editionlanguage="French">fr.wiktionary.org</edition><editionlanguage="Vietnamese">vi.wiktionary.org</edition><editionlanguage="Turkish">tr.wiktionary.org</edition><editionlanguage="Spanish">es.wiktionary.org</edition></editions></project></projects></Wikimedia>

The XPath expression

/Wikimedia/projects/project/@name

selects name attributes for all projects, and

/Wikimedia//editions

selects all editions of all projects, and

/Wikimedia/projects/project/editions/edition[@language='English']/text()

selects addresses of all English Wikimedia projects (text of alledition elements wherelanguage attribute is equal toEnglish). And the following

/Wikimedia/projects/project[@name='Wikipedia']/editions/edition/text()

selects addresses of all Wikipedias (text of alledition elements that exist underproject element with a name attribute ofWikipedia).

Implementations

[edit]

Command-line tools

[edit]

C/C++

[edit]

Free Pascal

[edit]
  • The unit XPath is included in the default libraries

Implementations for database engines

[edit]

Java

[edit]

TheJavapackagejavax.xml.xpath has been part of Java standard edition since Java 5[12] via theJava API for XML Processing. Technically this is an XPathAPI rather than an XPath implementation, and it allows the programmer the ability to select a specific implementation that conforms to the interface.

JavaScript

[edit]
  • Methoddocument.evaluate()[13] inweb browsers.
  • jQuery XPath plugin[14] based on Open-source XPath 2.0 implementation in JavaScript[15]
  • FontoXPath[16] Open source XPath 3.1 implementation in JavaScript. Currently under development.

.NET Framework

[edit]
  • In the System.Xml and System.Xml.XPath namespaces[17]
  • Sedna XML Database

Perl

[edit]
  • XML::LibXML[18] (libxml2)

PHP

[edit]
  • Sedna XML Database
  • DOMXPath[19] via libxml extension

Python

[edit]

Ruby

[edit]

Scheme

[edit]
  • Sedna XML Database

SQL

[edit]
  • MySQL supports a subset of XPath from version 5.1.5 onwards[23]
  • PostgreSQL supports XPath and XSLT from version 8.4 onwards[24]

Tcl

[edit]
  • The tDOM package provides a complete, compliant, and fast XPath implementation in C[25]

Use in schema languages

[edit]

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

  • The (nowISO standard) schema languageSchematron pioneered the approach.
  • A streaming subset of XPath is used in W3C XML Schema 1.0 for expressing uniqueness and key constraints. In XSD 1.1, the use of XPath is extended to support conditional type assignment based on attribute values, and to allow arbitrary Boolean assertions to be evaluated against the content of elements.
  • XForms uses XPath to bind types to values.
  • The approach has even found use in non-XML applications, such as the source code analyzer for Java calledPMD: the Java is converted to aDOM-like parse tree, then XPath rules are defined over the tree.

See also

[edit]

Notes

[edit]
  1. ^XPath 2.0 supports atomic types, defined as built-in types inXML Schema, and may also import user-defined types from a schema.
  2. ^XML authority Normal Walsh maintains an excellent online visualization of the axis specifiers.[7] It appears from the illustration thatpreceding,ancestor,self,descendant, andfollowing form a complete, ordered, non-overlapping partition of document element tree.
  3. ^For a complete description, seethe W3C Recommendation document.

References

[edit]
  1. ^ab"XML and Semantic Web W3C Standards Timeline"(PDF). 2012-02-04. Archived fromthe original(PDF) on 2013-04-24. Retrieved2012-02-04.
  2. ^Bergeron, Randy (2000-10-31)."XPath—Retrieving Nodes from an XML Document".SQL Server Magazine. Archived fromthe original on 2010-07-26. Retrieved2011-02-24.
  3. ^Pierre Geneves (2012)."Course: The XPath Language"(PDF).
  4. ^"XML Path Language (XPath) 3.0".World Wide Web Consortium (W3C). 2014-04-02. Retrieved2021-07-16.
  5. ^Kay, Michael (2012-02-10)."What's new in 3.0 (XSLT/XPath/XQuery) (plus XML Schema 1.1)"(PDF).XML Prague 2012. Retrieved2021-07-16.
  6. ^"XML Path Language (XPath) 3.1".World Wide Web Consortium (W3C). 2017-03-21. Retrieved2021-07-16.
  7. ^Walsh, Norman (1999)."Axis Specifiers".nwalsh.com. Personal blog of venerated XML sage graybeard. Retrieved2021-02-25.
  8. ^http://www.corefiling.com/opensource/pathan.html
  9. ^"pugixml.org - Home".pugixml.org. Retrieved2025-07-27.
  10. ^"XQilla : HomePage". Archived fromthe original on 2015-05-18. Retrieved2014-02-04.
  11. ^"Google Code Archive - Long-term storage for Google Code Project Hosting".code.google.com. Retrieved2025-07-27.
  12. ^"javax.xml.xpath (Java SE 10 & JDK 10)".Java® Platform, Standard Edition & Java Development Kit Version 10 API Specification. Retrieved2021-07-17.Since: 1.5
  13. ^"Document: evaluate() method - Web APIs | MDN".developer.mozilla.org. Retrieved2025-11-17.
  14. ^"GitHub - ilinsky/jquery-xpath: jQuery XPath plugin (with full XPath 2.0 language support)".github.com. Retrieved2025-07-27.
  15. ^"GitHub - ilinsky/xpath2.js: xpath.js - Open source XPath 2.0 implementation in JavaScript (DOM agnostic)".github.com. Retrieved2025-07-27.
  16. ^"GitHub - FontoXML/fontoxpath: A minimalistic XPath 3.1 implementation in pure JavaScript".github.com. Retrieved2025-07-27.
  17. ^"System.Xml Namespace".Microsoft Docs. 2020-10-25. Retrieved2021-07-16.
  18. ^"XML::LibXML - Perl Binding for libxml2 - metacpan.org".metacpan.org. Retrieved2025-07-27.
  19. ^"PHP: DOMXPath - Manual".www.php.net. Retrieved2025-07-27.
  20. ^"xml.etree.ElementTree â — The ElementTree XML API — Python 3.13.5 documentation".docs.python.org. Retrieved2025-07-27.
  21. ^"lxml - Processing XML and HTML with Python".lxml.de. Retrieved2025-07-27.
  22. ^Duke, Justin (2016-09-29)."How To Crawl A Web Page with Scrapy and Python 3".Digital Ocean. Retrieved2017-11-24.Selectors are patterns we can use to find one or more elements on a page so we can then work with the data within the element. scrapy supports either CSS selectors or XPath selectors.
  23. ^"MySQL :: MySQL 5.1 Reference Manual :: 12.11 XML Functions".dev.mysql.com. 2016-04-06. Archived from the original on 2016-04-06. Retrieved2021-07-17.
  24. ^"xml2".PostgreSQL Documentation. 2014-07-24. Retrieved2021-07-16.
  25. ^Loewer, Jochen (2000)."tDOM – A fast XML/DOM/XPath package for Tcl written in C"(PDF).Proceedings of First European TCL/Tk User Meeting. Retrieved16 July 2021.

External links

[edit]
Wikibooks has more on the topic of:XPath
Products,
standards
Recommendations
Notes
Working drafts
Guidelines
Initiative
Deprecated
Obsoleted
Groups,
organizations
Elected
Working
Community, business
Closed
Software
Browsers
Conferences
In current use
Proprietary
Superseded
International
National
Other
Retrieved from "https://en.wikipedia.org/w/index.php?title=XPath&oldid=1322659433"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp