Class XPath
java.lang.Object
org.libxmq.imp.xml.XPath
A compiled XPath 1.0 expression that is evaluated against the xmq DOM.
The engine behaves like the XPath implementation in libxml2 2.9.14
(xpath.c, Copyright (C) 1998-2012 Daniel Veillard, MIT license), since
the C version of xmq uses libxml2 and the java version must produce
identical results. This covers which expressions are accepted, the
traversal of the axes (eg dtd nodes are skipped and entity references
are never descended into), document order, the conversion of numbers
to strings, the comparison rules and the error behaviour (eg an
undefined namespace prefix or an unknown function is an error).
The result of an evaluation is one of: a List<XmlNode> (a node-set
in document order without duplicates), a String, a Double or a Boolean.
Namespace nodes in a node-set are
XPathNamespaceNodes.
Example:
XPath xp = XPath.compile("//item[@id='a']/name");
List<XmlNode> nodes = xp.selectNodes(doc);
-
Method Summary
Modifier and TypeMethodDescriptionstatic XPathCompile an expression with the default settings, the namespaces are given when evaluating.static XPathcompile(String expr, XPathSettings settings) Compile an expression, like xml_xpath_compile in the C version.Evaluate the expression with a fresh context, like xml_xpath_eval.booleanevaluateBoolean(XmlNode context) Evaluate as a boolean, like xpath boolean().booleanevaluateBoolean(XmlNode context, Map<String, String> namespaces) Evaluate as a boolean, like xpath boolean().doubleevaluateNumber(XmlNode context) Evaluate as a number, like xpath number().doubleevaluateNumber(XmlNode context, Map<String, String> namespaces) Evaluate as a number, like xpath number().evaluateString(XmlNode context) Evaluate as a string, like xpath string().evaluateString(XmlNode context, Map<String, String> namespaces) Evaluate as a string, like xpath string().Returns the source of the expression.static StringnumberToString(double d) Format a number like xpath string() (libxml2 xmlXPathFormatNumber).selectNodes(XmlNode context) Select the nodes of a node set expression.selectNodes(XmlNode context, Map<String, String> namespaces) Select the nodes of a node set expression.static doubleParse a number like xpath number() (libxml2 xmlXPathStringEvalNumber).toString()
-
Method Details
-
compile
Compile an expression with the default settings, the namespaces are given when evaluating.- Parameters:
expr- The expression.- Returns:
- The compiled expression.
- Throws:
XPathException- If the expression is invalid.
-
compile
Compile an expression, like xml_xpath_compile in the C version. The settings (and their namespaces) are copied, prefixes are resolved when the expression is evaluated.- Parameters:
expr- The expression.settings- The settings.- Returns:
- The compiled expression.
- Throws:
XPathException- If the expression is invalid.
-
expression
-
evaluate
Evaluate the expression with a fresh context, like xml_xpath_eval.- Parameters:
context- The context node.namespaces- Extra prefixes, or null.- Returns:
- A List<XmlNode>, String, Double or Boolean.
- Throws:
XPathException- On an error.
-
selectNodes
Select the nodes of a node set expression.- Parameters:
context- The context node.- Returns:
- The nodes in document order.
- Throws:
XPathException- On an error or if the value is not a node set.
-
selectNodes
public List<XmlNode> selectNodes(XmlNode context, Map<String, String> namespaces) throws XPathExceptionSelect the nodes of a node set expression.- Parameters:
context- The context node.namespaces- Extra prefixes, or null.- Returns:
- The nodes in document order.
- Throws:
XPathException- On an error or if the value is not a node set.
-
evaluateString
Evaluate as a string, like xpath string().- Parameters:
context- The context node.- Returns:
- The string.
- Throws:
XPathException- On an error.
-
evaluateString
Evaluate as a string, like xpath string().- Parameters:
context- The context node.namespaces- Extra prefixes, or null.- Returns:
- The string.
- Throws:
XPathException- On an error.
-
evaluateNumber
Evaluate as a number, like xpath number().- Parameters:
context- The context node.- Returns:
- The number.
- Throws:
XPathException- On an error.
-
evaluateNumber
Evaluate as a number, like xpath number().- Parameters:
context- The context node.namespaces- Extra prefixes, or null.- Returns:
- The number.
- Throws:
XPathException- On an error.
-
evaluateBoolean
Evaluate as a boolean, like xpath boolean().- Parameters:
context- The context node.- Returns:
- The boolean.
- Throws:
XPathException- On an error.
-
evaluateBoolean
public boolean evaluateBoolean(XmlNode context, Map<String, String> namespaces) throws XPathExceptionEvaluate as a boolean, like xpath boolean().- Parameters:
context- The context node.namespaces- Extra prefixes, or null.- Returns:
- The boolean.
- Throws:
XPathException- On an error.
-
numberToString
Format a number like xpath string() (libxml2 xmlXPathFormatNumber).- Parameters:
d- The number.- Returns:
- The string.
-
stringToNumber
Parse a number like xpath number() (libxml2 xmlXPathStringEvalNumber).- Parameters:
s- The string.- Returns:
- The number, NaN if not a number.
-
toString
-