Package net.sf.saxon.sxpath
Class XPathExpression
java.lang.Object
net.sf.saxon.sxpath.XPathExpression
- All Implemented Interfaces:
Serializable
This class is a representation of an XPath Expression for use with the
XPathEvaluator
class.
It is modelled on the XPath API defined in JAXP 1.3, but is cut down to remove any dependencies on JAXP 1.3,
making it suitable for use on vanilla JDK 1.4 installations.- Author:
- Michael H. Kay
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
XPathExpression
(XPathEvaluator evaluator, Expression exp) The constructor is protected, to ensure that instances can only be created using the createExpression() method of XPathEvaluator -
Method Summary
Modifier and TypeMethodDescriptionCreate a dynamic context suitable for evaluating this expression, without supplying a context itemcreateDynamicContext
(Controller controller, Item contextItem) Create a dynamic context suitable for evaluating this expression within an environment represented by an existing controller.createDynamicContext
(Item contextItem) Create a dynamic context suitable for evaluating this expressionboolean
effectiveBooleanValue
(XPathDynamicContext context) Evaluate the expression, returning its effective boolean valueExecute a prepared XPath expression, returning the results as a List in which items have been converted to the appropriate Java object.evaluate
(XPathDynamicContext context) Execute the expression, returning the result as a List, whose members will be instances of the classItem
evaluateSingle
(Source source) Execute a prepared XPath expression, returning the first item in the result.evaluateSingle
(XPathDynamicContext context) Execute the expression, returning the result as a singleItem
If the result of the expression is a sequence containing more than one item, items after the first are discarded.Low-level method to get the internal Saxon expression object.SequenceIterator
<? extends Item> iterate
(XPathDynamicContext context) Execute the expression, returning the result as aSequenceIterator
, whose members will be instances of the classItem
rawIterator
(Source source) Deprecated.protected void
setStackFrameMap
(SlotManager map, int numberOfExternalVariables) Define the number of slots needed for local variables within the expression
-
Constructor Details
-
XPathExpression
The constructor is protected, to ensure that instances can only be created using the createExpression() method of XPathEvaluator- Parameters:
evaluator
- the creating XPathEvaluatorexp
- the internal representation of the compiled expression
-
-
Method Details
-
setStackFrameMap
Define the number of slots needed for local variables within the expression- Parameters:
map
- the stack frame map identifying all the variables used by the expression, both those declared internally, and references to variables declared externallynumberOfExternalVariables
- the number of slots in the stack frame allocated to externally-declared variables. These must be assigned a value before execution starts
-
createDynamicContext
Create a dynamic context suitable for evaluating this expression, without supplying a context item- Returns:
- an XPathDynamicContext object representing a suitable dynamic context. This will be initialized with a stack frame suitable for holding the variables used by the expression.
-
createDynamicContext
Create a dynamic context suitable for evaluating this expression- Parameters:
contextItem
- the initial context item, which may be null if no context item is required, or if it is to be supplied later- Returns:
- an XPathDynamicContext object representing a suitable dynamic context. This will be initialized with a stack frame suitable for holding the variables used by the expression.
- Throws:
XPathException
- if the context item does not match the required context item type set up in the static context
-
createDynamicContext
public XPathDynamicContext createDynamicContext(Controller controller, Item contextItem) throws XPathException Create a dynamic context suitable for evaluating this expression within an environment represented by an existing controller. The dynamic context will inherit properties of the Controller such as the current error listener, URI resolver, document pool, and implicit timezone.- Parameters:
controller
- an existing controller. May be null, in which case the method is equivalent to callingcreateDynamicContext(net.sf.saxon.om.Item)
contextItem
- the initial context item, which may be null if no context item is required, or if it is to be supplied later- Returns:
- an XPathDynamicContext object representing a suitable dynamic context. This will be initialized with a stack frame suitable for holding the variables used by the expression.
- Throws:
XPathException
- if the context item does not match the required context item type set up in the static context- Since:
- 9.2
-
iterate
Execute the expression, returning the result as aSequenceIterator
, whose members will be instances of the classItem
Note that if evaluation of the expression fails with a dynamic error, this will generally be reported in the form of an exception thrown by the next() method of the
SequenceIterator
- Parameters:
context
- the XPath dynamic context- Returns:
- an iterator over the result of the expression
- Throws:
XPathException
- if a dynamic error occurs during the expression evaluation (though in many cases, the error will only be detected when the iterator is actually used).
-
evaluate
Execute the expression, returning the result as a List, whose members will be instances of the classItem
- Parameters:
context
- the XPath dynamic context- Returns:
- a list of items representing the result of the expression
- Throws:
XPathException
- if a dynamic error occurs while evaluation the expression
-
evaluateSingle
Execute the expression, returning the result as a singleItem
If the result of the expression is a sequence containing more than one item, items after the first are discarded. If the result is an empty sequence, the method returns null.- Parameters:
context
- the XPath dynamic context- Returns:
- the first item in the result of the expression, or null to indicate that the result is an empty sequence
- Throws:
XPathException
- if a dynamic error occurs during the expression evaluation
-
effectiveBooleanValue
Evaluate the expression, returning its effective boolean value- Parameters:
context
- the XPath dynamic context- Returns:
- the effective boolean value of the result, as defined by the fn:boolean() function
- Throws:
XPathException
- if a dynamic error occurs, or if the result is a value for which no effective boolean value is defined
-
evaluate
Execute a prepared XPath expression, returning the results as a List in which items have been converted to the appropriate Java object.- Parameters:
source
- the document or other node against which the XPath expression will be evaluated. This may be a Saxon NodeInfo object, representing a node in an existing tree, or it may be any kind of JAXP Source object such as a StreamSource SAXSource or DOMSource. For the way in which the source document is built, seeConfiguration.buildDocument(javax.xml.transform.Source)
- Returns:
- The results of the expression, as a List. The List represents the sequence of items returned by the expression. Each item in the list will either be an instance of net.sf.saxon.om.NodeInfo, representing a node, or a Java object representing an atomic value.
- Throws:
XPathException
- if a dynamic error occurs during the expression evaluation
-
evaluateSingle
Execute a prepared XPath expression, returning the first item in the result. This is useful where it is known that the expression will only return a singleton value (for example, a single node, or a boolean).- Parameters:
source
- the document or other node against which the XPath expression will be evaluated. This may be a Saxon NodeInfo object, representing a node in an existing tree, or it may be any kind of JAXP Source object such as a StreamSource SAXSource or DOMSource. For the way in which the source document is built, seeConfiguration.buildDocument(javax.xml.transform.Source)
- Returns:
- The first item in the sequence returned by the expression. If the expression returns an empty sequence, this method returns null. Otherwise, it returns the first item in the result sequence, represented as a Java object using the same mapping as for the evaluate() method
- Throws:
XPathException
- if a dynamic error occurs during the expression evaluation
-
rawIterator
Deprecated.since 8.9 - useiterate(net.sf.saxon.sxpath.XPathDynamicContext)
Get a raw iterator over the results of the expression. This returns results without any conversion of the returned items to "native" Java classes. This method is intended for use by applications that need to process the results of the expression using internal Saxon interfaces.- Parameters:
source
- the document or other node against which the XPath expression will be evaluated. This may be a Saxon NodeInfo object, representing a node in an existing tree, or it may be any kind of JAXP Source object such as a StreamSource SAXSource or DOMSource. For the way in which the source document is built, seeConfiguration.buildDocument(javax.xml.transform.Source)
- Returns:
- an iterator over the results of the expression
- Throws:
XPathException
- if a dynamic error occurs during the expression evaluation
-
getInternalExpression
Low-level method to get the internal Saxon expression object. This exposes a wide range of internal methods that may be needed by specialized applications, and allows greater control over the dynamic context for evaluating the expression.- Returns:
- the underlying Saxon expression object.
-
iterate(net.sf.saxon.sxpath.XPathDynamicContext)