public class XPath
extends java.lang.Object
XPath
/Modifier and Type | Field and Description |
---|---|
static java.lang.String |
SYSPROP_USE_SAXON_XPATH
"interlok.useSaxonXPath" controls whether Saxon is explicitly enabled as an
XPathFactory (defaults to true). |
Constructor and Description |
---|
XPath() |
XPath(javax.xml.namespace.NamespaceContext ctx) |
XPath(javax.xml.namespace.NamespaceContext ctx,
javax.xml.xpath.XPathFactory factory) |
Modifier and Type | Method and Description |
---|---|
static javax.xml.xpath.XPathFactory |
newXPathFactory()
Convenience method to create a new
XPathFactory . |
static XPath |
newXPathInstance(DocumentBuilderFactoryBuilder builder,
javax.xml.namespace.NamespaceContext namespaceCtx)
Create a new
XPath instance. |
static XPath |
newXPathInstance(javax.xml.parsers.DocumentBuilderFactory builder,
javax.xml.namespace.NamespaceContext namespaceCtx) |
java.lang.String[] |
selectMultipleTextItems(org.w3c.dom.Node context,
java.lang.String xpath)
returns an array of string values taken from a list of elements returned by
an xpath
|
org.w3c.dom.NodeList |
selectNodeList(org.w3c.dom.Node context,
java.lang.String xpath)
selects a list of Nodes from the context node using the supplied xpath
|
org.w3c.dom.Node |
selectSingleNode(org.w3c.dom.Node context,
java.lang.String xpath)
Selects a single Node based on the supplied Xpath
|
java.lang.String |
selectSingleTextItem(org.w3c.dom.Node context,
java.lang.String xpath)
returns the string value contained in an element returned by an XPath
|
public static final java.lang.String SYSPROP_USE_SAXON_XPATH
XPathFactory
(defaults to true).
From the saxon documentation :
Saxon therefore no longer identifies itself (in the JAR file manifest) as a JAXP XPath supplier. If you want to load
Saxon as your XPath engine, you need to select it explicitly; it's not enough to just put it on the classpath.. If set
to true, then we attempt to use com.saxonica.config.EnterpriseXPathFactory
,
com.saxonica.config.ProfessionalXPathFactory
and net.sf.saxon.xpath.XPathFactoryImpl
as XPathFactory instances
(in that order).
public XPath()
public XPath(javax.xml.namespace.NamespaceContext ctx)
public XPath(javax.xml.namespace.NamespaceContext ctx, javax.xml.xpath.XPathFactory factory)
public java.lang.String selectSingleTextItem(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
context
- the node to apply the XPath toxpath
- the xpath to applyjavax.xml.xpath.XPathExpressionException
- on errorpublic java.lang.String[] selectMultipleTextItems(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
context
- the node to apply the XPath toxpath
- the xpath to applyjavax.xml.xpath.XPathExpressionException
- on errorpublic org.w3c.dom.NodeList selectNodeList(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
context
- the root node to queryxpath
- the xpath to applyjavax.xml.xpath.XPathExpressionException
- on error.public org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
context
- the root node to queryxpath
- the xpath to applyjavax.xml.xpath.XPathExpressionException
- on error.public static javax.xml.xpath.XPathFactory newXPathFactory()
XPathFactory
.XPathFactory.newInstance()
public static XPath newXPathInstance(DocumentBuilderFactoryBuilder builder, javax.xml.namespace.NamespaceContext namespaceCtx)
XPath
instance.
If the DocumentBuilderFactoryBuilder
has been explicitly set to be not namespace aware and the document does in fact
contain namespaces, then Saxon can cause merry havoc in the sense that //NonNamespaceXpath
doesn't work if the document
has namespaces in it. This then is a helper to mitigate against that; but of course in this situation you end up not being able
to use XPath 2.0 functions, when namespace-awareness is off as saxon 9.7+ no longer registers itself as a XPathFactory.
This leads us to a behavioural matrix that looks like this. Using Saxon means you get XPath 2.0 and its associated benefits,
Using XPathFactory.newInstance()
generally means you don't, unless you have registered an alternative XPathFactory.
DocumentBuilderFactoryBuilder#withNamespaceAware() | Namespace Context available | Uses Saxon | Uses XPathFactory#newInstance() | |||
---|---|---|---|---|---|---|
true | no | true | false | |||
true | true | true | false | |||
false | no | false | true | |||
false | yes | false | true | |||
not specified | yes | true | false | |||
not specified | no | true | false |
builder
- the document builder factorynamespaceCtx
- the namespace context (might be null, but we pass it into the XPath constructor)public static XPath newXPathInstance(javax.xml.parsers.DocumentBuilderFactory builder, javax.xml.namespace.NamespaceContext namespaceCtx)