Class XPath
- java.lang.Object
-
- com.adaptris.util.text.xml.XPath
-
public class XPath extends java.lang.Object
Wrapper aroundXPath
/
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
SYSPROP_USE_SAXON_XPATH
"interlok.useSaxonXPath" controls whether Saxon is explicitly enabled as anXPathFactory
(defaults to true).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static javax.xml.xpath.XPathFactory
newXPathFactory()
Convenience method to create a newXPathFactory
.static XPath
newXPathInstance(DocumentBuilderFactoryBuilder builder, javax.xml.namespace.NamespaceContext namespaceCtx)
Create a newXPath
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 xpathorg.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 xpathorg.w3c.dom.Node
selectSingleNode(org.w3c.dom.Node context, java.lang.String xpath)
Selects a single Node based on the supplied Xpathjava.lang.String
selectSingleTextItem(org.w3c.dom.Node context, java.lang.String xpath)
returns the string value contained in an element returned by an XPath
-
-
-
Field Detail
-
SYSPROP_USE_SAXON_XPATH
public static final java.lang.String SYSPROP_USE_SAXON_XPATH
"interlok.useSaxonXPath" controls whether Saxon is explicitly enabled as anXPathFactory
(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
andnet.sf.saxon.xpath.XPathFactoryImpl
as XPathFactory instances (in that order).- See Also:
- Constant Field Values
-
-
Method Detail
-
selectSingleTextItem
public java.lang.String selectSingleTextItem(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
returns the string value contained in an element returned by an XPath- Parameters:
context
- the node to apply the XPath toxpath
- the xpath to apply- Returns:
- the string extracted
- Throws:
javax.xml.xpath.XPathExpressionException
- on error
-
selectMultipleTextItems
public java.lang.String[] selectMultipleTextItems(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
returns an array of string values taken from a list of elements returned by an xpath- Parameters:
context
- the node to apply the XPath toxpath
- the xpath to apply- Returns:
- the strings extracted
- Throws:
javax.xml.xpath.XPathExpressionException
- on error
-
selectNodeList
public org.w3c.dom.NodeList selectNodeList(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
selects a list of Nodes from the context node using the supplied xpath- Parameters:
context
- the root node to queryxpath
- the xpath to apply- Returns:
- NodeList of returned Nodes
- Throws:
javax.xml.xpath.XPathExpressionException
- on error.
-
selectSingleNode
public org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node context, java.lang.String xpath) throws javax.xml.xpath.XPathExpressionException
Selects a single Node based on the supplied Xpath- Parameters:
context
- the root node to queryxpath
- the xpath to apply- Returns:
- the Node extracted
- Throws:
javax.xml.xpath.XPathExpressionException
- on error.
-
newXPathFactory
public static javax.xml.xpath.XPathFactory newXPathFactory()
Convenience method to create a newXPathFactory
.- Returns:
- either a Saxon based XPathFactory or one auto-found by
XPathFactory.newInstance()
-
newXPathInstance
public static XPath newXPathInstance(DocumentBuilderFactoryBuilder builder, javax.xml.namespace.NamespaceContext namespaceCtx)
Create a newXPath
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 - Parameters:
builder
- the document builder factorynamespaceCtx
- the namespace context (might be null, but we pass it into the XPath constructor)
-
newXPathInstance
public static XPath newXPathInstance(javax.xml.parsers.DocumentBuilderFactory builder, javax.xml.namespace.NamespaceContext namespaceCtx)
-
-