public class XPath
extends java.lang.Object
Applications can select nodes directly from the XPath facade.
For example,
Node verse = XPath.find("chapter/verse", node);
For greater efficiency, applications can also precompile the
match patterns.
Pattern pattern = XPath.parseSelect("chapter/verse");
Node verse = pattern.find(node);
XPath can also return values based on XPath expressions, following
the XPath expression syntax. Applications can use the expressions for
the equivalent of xsl:value-of
Expr expr = XPath.parseExpr("chapter/verse/@id + 1");
double value = expr.evalNumber(node);
To support the XPath pattern variables, XPath uses an environment object. Most applications will not need to use it.
Modifier and Type | Method and Description |
---|---|
static Env |
createCall(Env parent)
Creates a new variable environment based on an old environment.
|
static Env |
createEnv()
Creates a new variable environment.
|
static Env |
createEnv(Env global)
Creates a new variable environment based on an old environment.
|
static boolean |
evalBoolean(java.lang.String query,
org.w3c.dom.Node node)
Evaluates an XPath expression, returning a boolean.
|
static double |
evalNumber(java.lang.String query,
org.w3c.dom.Node node)
Evaluates an XPath expression, returning a double.
|
static java.lang.Object |
evalObject(java.lang.String query,
org.w3c.dom.Node node)
Evaluates an XPath expression, returning an object
|
static java.lang.String |
evalString(java.lang.String query,
org.w3c.dom.Node node)
Evaluates an XPath expression, returning a string.
|
static org.w3c.dom.Node |
find(java.lang.String query,
org.w3c.dom.Node node)
Finds a node based on an XPath pattern.
|
static void |
freeEnv(Env env)
Free an environment.
|
static Expr |
parseExpr(java.lang.String query)
Parses an XPath expression for later evaluation.
|
static Expr |
parseExpr(java.lang.String query,
NamespaceContext namespace)
Parses an XPath expression for later evaluation.
|
static Expr |
parseExpr(java.lang.String query,
NamespaceContext namespace,
AbstractPattern nodeList)
Parses an XPath expression for later evaluation.
|
static Pattern |
parseMatch(java.lang.String query)
Create a node match pattern.
|
static Pattern |
parseMatch(java.lang.String query,
NamespaceContext namespace)
Create a node match pattern.
|
static Pattern |
parseSelect(java.lang.String query)
Create a node selection pattern.
|
static Pattern |
parseSelect(java.lang.String query,
NamespaceContext namespace)
Create a node selection pattern.
|
static java.util.Iterator |
select(java.lang.String query,
org.w3c.dom.Node node)
Selects all node matching an XPath pattern
|
public static org.w3c.dom.Node find(java.lang.String query, org.w3c.dom.Node node) throws XPathException
XPath.find("child", node)
will find children,
not grandchildren.query
- XPath select pattern.node
- XML node to start searching from.XPathException
public static java.util.Iterator select(java.lang.String query, org.w3c.dom.Node node) throws XPathException
query
- XPath select pattern.node
- XML node to start searching from.XPathException
public static Pattern parseSelect(java.lang.String query) throws XPathParseException
query
- XPath select pattern.XPathParseException
public static Pattern parseSelect(java.lang.String query, NamespaceContext namespace) throws XPathParseException
XSLT uses this version of parseSelect for proper namespace matching.
query
- XPath select pattern.namespace
- the appropriate namespace mappingsXPathParseException
public static Pattern parseMatch(java.lang.String query) throws XPathParseException
query
- XPath match pattern.XPathParseException
public static Pattern parseMatch(java.lang.String query, NamespaceContext namespace) throws XPathParseException
query
- XPath match pattern.namespace
- the appropriate namespace mappings.XPathParseException
public static java.lang.String evalString(java.lang.String query, org.w3c.dom.Node node) throws XPathException
value-of
element.
For example, to get the value of an attribute use:
String value = XPath.evalString("@id", node);
query
- XPath expressionnode
- the node contextXPathException
public static double evalNumber(java.lang.String query, org.w3c.dom.Node node) throws XPathException
query
- XPath expressionnode
- the node contextXPathException
public static boolean evalBoolean(java.lang.String query, org.w3c.dom.Node node) throws XPathException
query
- XPath expressionnode
- the node contextXPathException
public static java.lang.Object evalObject(java.lang.String query, org.w3c.dom.Node node) throws XPathException
query
- XPath expressionnode
- the node contextXPathException
public static Expr parseExpr(java.lang.String query) throws XPathParseException
query
- XPath expressionXPathParseException
public static Expr parseExpr(java.lang.String query, NamespaceContext namespace) throws XPathParseException
query
- XPath expressionnamespace
- namespace contextXPathParseException
public static Expr parseExpr(java.lang.String query, NamespaceContext namespace, AbstractPattern nodeList) throws XPathParseException
query
- XPath expressionnamespace
- namespace contextnodeList
- containing nodeList patternXPathParseException
public static Env createEnv()
public static Env createEnv(Env global)
This lets environments share globals even through function calls.
public static Env createCall(Env parent)
This lets environments share globals even through function calls.
public static void freeEnv(Env env)