Package javax.xml.xpath
See: Description
-
Interface Summary Interface Description XPath XPath
provides access to the XPath evaluation environment and expressions.XPathExpression XPathExpression
provides access to compiled XPath expressions.XPathFunction XPathFunction
provides access to XPath functions.XPathFunctionResolver XPathFunctionResolver
provides access to the set of user definedXPathFunction
s.XPathVariableResolver XPathVariableResolver
provides access to the set of user defined XPath variables. -
Class Summary Class Description XPathConstants XPath constants.XPathFactory AnXPathFactory
instance can be used to createXPath
objects. -
Exception Summary Exception Description XPathException XPathException
represents a generic XPath exception.XPathExpressionException XPathExpressionException
represents an error in an XPath expression.XPathFactoryConfigurationException XPathFactoryConfigurationException
represents a configuration error in aXPathFactory
environment.XPathFunctionException XPathFunctionException
represents an error with an XPath function.
Package javax.xml.xpath Description
This package provides an object-model neutral API for the evaluation of XPath expressions and access to the evaluation environment.
The following XML standards apply:
XPath Overview
The XPath language provides a simple, concise syntax for selecting nodes from an XML document. XPath also provides rules for converting a node in an XML document object model (DOM) tree to a boolean, double, or string value. XPath is a W3C-defined language and an official W3C recommendation; the W3C hosts the XML Path Language (XPath) Version 1.0 specification.
XPath started in life in 1999 as a supplement to the XSLT and XPointer languages, but has more recently become popular as a stand-alone language, as a single XPath expression can be used to replace many lines of DOM API code.
XPath Expressions
An XPath expression is composed of a location path and one or more optional predicates. Expressions may also include XPath variables.
The following is an example of a simple XPath expression:
/foo/bar
This example would select the <bar>
element in
an XML document such as the following:
<foo> <bar/> </foo>
The expression /foo/bar
is an example of a location
path. While XPath location paths resemble Unix-style file system
paths, an important distinction is that XPath expressions return
all nodes that match the expression. Thus, all three
<bar>
elements in the following document would be
selected by the /foo/bar
expression:
<foo> <bar/> <bar/> <bar/> </foo>
A special location path operator, //
, selects nodes at
any depth in an XML document. The following example selects all
<bar>
elements regardless of their location in a
document:
//bar
A wildcard operator, *, causes all element nodes to be selected.
The following example selects all children elements of a
<foo>
element:
/foo/*
In addition to element nodes, XPath location paths may also address attribute nodes, text nodes, comment nodes, and processing instruction nodes. The following table gives examples of location paths for each of these node types:
Location Path | Description |
/foo/bar/@id
|
Selects the attribute id of the <bar> element
|
/foo/bar/text()
|
Selects the text nodes of the <bar> element. No
distinction is made between escaped and non-escaped character data.
|
/foo/bar/comment()
|
Selects all comment nodes contained in the <bar> element.
|
/foo/bar/processing-instruction()
|
Selects all processing-instruction nodes contained in the
<bar> element.
|
Predicates allow for refining the nodes selected by an XPath
location path. Predicates are of the form
[expression]
. The following example selects all
<foo>
elements that contain an include
attribute with the value of true
:
//foo[@include='true']
Predicates may be appended to each other to further refine an expression, such as:
//foo[@include='true'][@mode='bar']
Using the XPath API
The following example demonstrates using the XPath API to select one or more nodes from an XML document:
XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; InputSource inputSource = new InputSource("widgets.xml"); NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
XPath Expressions and Types
While XPath expressions select nodes in the XML document, the XPath API allows the selected nodes to be coalesced into one of the following other data types:
Boolean
Number
String
The desired return type is specified by a QName
parameter in method call used to evaluate
the expression, which is either a call to
XPathExpression.evalute(...)
or to one of the
XPath.evaluate(...)
convenience methods. The allowed
QName values are specified as constants in the XPathConstants
class; they are:
XPathConstants.NODESET
XPathConstants.NODE
XPathConstants.STRING
XPathConstants.BOOLEAN
XPathConstants.NUMBER
When a Boolean
return type is requested,
Boolean.TRUE
is returned if one or more nodes were
selected; otherwise, Boolean.FALSE
is returned.
The String
return type is a convenience for retrieving
the character data from a text node, attribute node, comment node, or
processing-instruction node. When used on an element node, the value
of the child text nodes is returned.
The Number
return type attempts to coalesce the text
of a node to a double
data type.
XPath Context
XPath location paths may be relative to a particular node in the
document, known as the context
. Consider the following
XML document:
<widgets> <widget> <manufacturer/> <dimensions/> </widget> </widgets>
The <widget>
element can be selected with the
following XPath API code:
// parse the XML as a W3C Document DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File("/widgets.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
With a reference to the <widget>
element, a
relative XPath expression can now written to select the
<manufacturer>
child element:
XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "manufacturer"; Node manufacturerNode = (Node) xpath.evaluate(expression, widgetNode, XPathConstants.NODE);
- Author Ben Galbraith
- Author Norman Walsh
- Author Jeff Suttor
- See XML Path Language (XPath) Version 1.0
- Since 1.5
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 11/06/2005, zuletzt geändert 04/03/2020
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-rf-javax/xml/xpath/package-summary.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.