Package net.sf.saxon.xpath
Class StandaloneContext
java.lang.Object
net.sf.saxon.xpath.StandaloneContext
- All Implemented Interfaces:
StaticContext
,NamespaceResolver
A StandaloneContext provides a context for parsing an XPath expression
in a context other than a stylesheet. In particular, it is used to support
the JAXP 1.3 XPath API. This API does not actually expose the StaticContext
object directly; rather, the static context (namespaces, variables, and functions)
is manipulated through the XPath object, implemented in Saxon by the
XPathEvaluator
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a StandaloneContext using the default Configuration and NamePoolStandaloneContext
(Configuration config) Create a StandaloneContext using a specific Configuration.StandaloneContext
(NodeInfo node) Create a StandaloneContext using a specific Node. -
Method Summary
Modifier and TypeMethodDescriptionbindVariable
(int fingerprint) Bind a variable used in an XPath Expression to the XSLVariable element in which it is declared.void
Clear all the declared namespaces, including the standard ones (xml, xslt, saxon).void
Clear all the declared namespaces, except for the standard ones (xml, xslt, saxon, xdt).void
declareCollation
(String name, Comparator comparator, boolean isDefault) Declare a named collationvoid
declareNamespace
(String prefix, String uri) Declare a namespace whose prefix can be used in expressions.declareVariable
(String qname, Object initialValue) Declare a variable.Get the Base URI of the stylesheet element, for resolving any relative URI's used in the expression.getCollation
(String name) Get a named collation.Get the system configurationGet the name of the default collation.short
Get the default XPath namespace, as a namespace code that can be looked up in the NamePoolGet the default function namespaceGet the function library containing all the in-scope functions available in this static contextGet the set of imported schemasint
Get the line number of the expression within that container.Get the location map.Get the NamePool used for compiling expressionsGet the NamespaceContext that was set usingsetNamespaceContext(javax.xml.namespace.NamespaceContext)
Get a namespace resolver to resolve the namespaces declared in this static context.Get the stack frame map containing the slot number allocations for the variables declared in this static contextGet the system ID of the container of the expression.getURIForPrefix
(String prefix) Get the URI for a prefix, using the declared namespaces as the context for namespace resolution.getURIForPrefix
(String prefix, boolean useDefault) Get the namespace URI corresponding to a given prefix.Get the XPathVariableResolvervoid
importSchema
(Source source) Import a schema.boolean
Determine whether a built-in type is available in this context.boolean
isImportedSchema
(String namespace) Determine whether a Schema for a given target namespace has been imported.boolean
Determine whether Backwards Compatible Mode is usedvoid
issueWarning
(String s, SourceLocator locator) Issue a compile-time warning.Get an iterator over all the prefixes declared in this namespace context.Construct a dynamic context for early evaluation of constant subexpressionsvoid
setBackwardsCompatibilityMode
(boolean backwardsCompatible) Set XPath 1.0 backwards compatibility modevoid
setBaseURI
(String baseURI) Set the base URI in the static contextvoid
Set the default namespace for element and type namesvoid
Set the default function namespacevoid
Set the function library to be usedvoid
setLocationMap
(LocationMap locationMap) void
setNamespaceContext
(NamespaceContext context) Supply the NamespaceContext used to resolve namespaces.void
setNamespaces
(NodeInfo node) Set all the declared namespaces to be the namespaces that are in-scope for a given node.void
setXPathFunctionResolver
(XPathFunctionResolver xPathFunctionResolver) void
Set an XPathVariableResolver.
-
Constructor Details
-
StandaloneContext
public StandaloneContext()Create a StandaloneContext using the default Configuration and NamePool -
StandaloneContext
Create a StandaloneContext using a specific Configuration.- Parameters:
config
- the Configuration. For schema-aware XPath expressions, this must be a SchemaAwareConfiguration.
-
StandaloneContext
Create a StandaloneContext using a specific Node. This node is used to initialize the NamePool and also to establish the initial set of in-scope namespaces.
-
-
Method Details
-
getConfiguration
Get the system configuration- Specified by:
getConfiguration
in interfaceStaticContext
-
makeEarlyEvaluationContext
Construct a dynamic context for early evaluation of constant subexpressions- Specified by:
makeEarlyEvaluationContext
in interfaceStaticContext
-
getLocationMap
Description copied from interface:StaticContext
Get the location map. This is a mapping from short location ids held with each expression or subexpression, to a fully-resolved location in a source stylesheet or query.- Specified by:
getLocationMap
in interfaceStaticContext
-
setLocationMap
-
declareNamespace
Declare a namespace whose prefix can be used in expressions. Namespaces may either be pre-declared (the traditional Saxon interface), or they may be resolved on demand using a supplied NamespaceContext. When a prefix has to be resolved, the parser looks first in the pre-declared namespaces, then in the supplied NamespaceContext object.- Parameters:
prefix
- The namespace prefix. Must not be null. Must not be the empty string ("") - unqualified names in an XPath expression always refer to the null namespace.uri
- The namespace URI. Must not be null.
-
setNamespaceContext
Supply the NamespaceContext used to resolve namespaces. This supplements namespaces that have been explicitly declared usingdeclareNamespace(java.lang.String, java.lang.String)
or that have been implicitly declared usingsetNamespaces(net.sf.saxon.om.NodeInfo)
-
getNamespaceContext
Get the NamespaceContext that was set usingsetNamespaceContext(javax.xml.namespace.NamespaceContext)
-
clearNamespaces
public void clearNamespaces()Clear all the declared namespaces, except for the standard ones (xml, xslt, saxon, xdt). This doesn't clear the namespace context set usingsetNamespaceContext(javax.xml.namespace.NamespaceContext)
-
clearAllNamespaces
public void clearAllNamespaces()Clear all the declared namespaces, including the standard ones (xml, xslt, saxon). Leave only the XML namespace and the default namespace (xmlns="") -
setNamespaces
Set all the declared namespaces to be the namespaces that are in-scope for a given node. In addition, the standard namespaces (xml, xslt, saxon) are declared.- Parameters:
node
- The node whose in-scope namespaces are to be used as the context namespaces. Note that this will have no effect unless this node is an element.
-
setBaseURI
Set the base URI in the static context -
declareCollation
Declare a named collation- Parameters:
name
- The name of the collation (technically, a URI)comparator
- The Java Comparator used to implement the collating sequenceisDefault
- True if this is to be used as the default collation
-
getStackFrameMap
Get the stack frame map containing the slot number allocations for the variables declared in this static context -
declareVariable
Declare a variable. A variable may be declared before an expression referring to it is compiled. Alternatively, a JAXP XPathVariableResolver may be supplied to perform the resolution. A variable that has been explicitly declared is used in preference.- Parameters:
qname
- Lexical QName identifying the variable. The namespace prefix, if any, must have been declared before this method is called, or must be resolvable using the namespace context.initialValue
- The initial value of the variable. A Java object that can be converted to an XPath value.- Throws:
XPathException
-
setXPathVariableResolver
Set an XPathVariableResolver. This is used to resolve variable references if no variable has been explicitly declared.- Parameters:
resolver
- A JAXP 1.3 XPathVariableResolver
-
getXPathVariableResolver
Get the XPathVariableResolver -
setXPathFunctionResolver
-
getXPathFunctionResolver
-
getNamePool
Get the NamePool used for compiling expressions- Specified by:
getNamePool
in interfaceStaticContext
-
issueWarning
Issue a compile-time warning. This method is used during XPath expression compilation to output warning conditions. The default implementation writes the message to System.err. To change the destination of messages, create a subclass of StandaloneContext that overrides this method.- Specified by:
issueWarning
in interfaceStaticContext
-
getSystemId
Get the system ID of the container of the expression. Used to construct error messages.- Specified by:
getSystemId
in interfaceStaticContext
- Returns:
- "" always
-
getBaseURI
Get the Base URI of the stylesheet element, for resolving any relative URI's used in the expression. Used by the document() function, resolve-uri(), etc.- Specified by:
getBaseURI
in interfaceStaticContext
- Returns:
- "" if no base URI has been set
-
getLineNumber
public int getLineNumber()Get the line number of the expression within that container. Used to construct error messages.- Specified by:
getLineNumber
in interfaceStaticContext
- Returns:
- -1 always
-
getURIForPrefix
Get the URI for a prefix, using the declared namespaces as the context for namespace resolution. The default namespace is NOT used when the prefix is empty. This method is provided for use by the XPath parser.- Specified by:
getURIForPrefix
in interfaceStaticContext
- Parameters:
prefix
- The prefix- Throws:
XPathException
- if the prefix is not declared
-
getNamespaceResolver
Description copied from interface:StaticContext
Get a namespace resolver to resolve the namespaces declared in this static context.- Specified by:
getNamespaceResolver
in interfaceStaticContext
- Returns:
- a namespace resolver.
-
getURIForPrefix
Get the namespace URI corresponding to a given prefix. Return null if the prefix is not in scope. This method first searches any namespaces declared usingdeclareNamespace(String, String)
, and then searches any namespace context supplied usingsetNamespaceContext(javax.xml.namespace.NamespaceContext)
.- Specified by:
getURIForPrefix
in interfaceNamespaceResolver
- Parameters:
prefix
- the namespace prefixuseDefault
- true if the default namespace is to be used when the prefix is ""- Returns:
- the uri for the namespace, or null if the prefix is not in scope. Return "" if the prefix maps to the null namespace.
-
iteratePrefixes
Get an iterator over all the prefixes declared in this namespace context. This will include the default namespace (prefix="") and the XML namespace where appropriate. The iterator only covers namespaces explicitly declared usingdeclareNamespace(String, String)
; it does not include namespaces declared usingsetNamespaceContext(javax.xml.namespace.NamespaceContext)
, because the JAXPNamespaceContext
class provides no way to discover all the namespaces available.- Specified by:
iteratePrefixes
in interfaceNamespaceResolver
-
bindVariable
Bind a variable used in an XPath Expression to the XSLVariable element in which it is declared. This method is provided for use by the XPath parser, and it should not be called by the user of the API, or overridden, unless variables are to be declared using a mechanism other than the declareVariable method of this class.If the variable has been explicitly declared using
declareVariable(String, Object)
, that value is used; otherwise if a variable resolved has been supplied usingsetXPathVariableResolver(javax.xml.xpath.XPathVariableResolver)
then that is used.- Specified by:
bindVariable
in interfaceStaticContext
- Parameters:
fingerprint
- the name of the variable- Returns:
- a VariableReference representing the variable reference, suitably initialized to refer to the corresponding variable declaration
- Throws:
StaticError
- If no variable with the given name is found, or if the value supplied for the variable cannot be converted to an XPath value.
-
getFunctionLibrary
Get the function library containing all the in-scope functions available in this static context- Specified by:
getFunctionLibrary
in interfaceStaticContext
-
setFunctionLibrary
Set the function library to be used -
getCollation
Get a named collation.- Specified by:
getCollation
in interfaceStaticContext
- Parameters:
name
- The name of the required collation. Supply null to get the default collation.- Returns:
- the collation identified by the given name, as set previously using declareCollation. Return null if no collation with this name is found.
-
getDefaultCollationName
Get the name of the default collation.- Specified by:
getDefaultCollationName
in interfaceStaticContext
- Returns:
- the name of the default collation; or the name of the codepoint collation if no default collation has been defined
-
setDefaultElementNamespace
Set the default namespace for element and type names -
getDefaultElementNamespace
public short getDefaultElementNamespace()Get the default XPath namespace, as a namespace code that can be looked up in the NamePool- Specified by:
getDefaultElementNamespace
in interfaceStaticContext
-
setDefaultFunctionNamespace
Set the default function namespace -
getDefaultFunctionNamespace
Get the default function namespace- Specified by:
getDefaultFunctionNamespace
in interfaceStaticContext
-
setBackwardsCompatibilityMode
public void setBackwardsCompatibilityMode(boolean backwardsCompatible) Set XPath 1.0 backwards compatibility mode- Parameters:
backwardsCompatible
- if true, expressions will be evaluated with XPath 1.0 compatibility mode set to true.
-
isInBackwardsCompatibleMode
public boolean isInBackwardsCompatibleMode()Determine whether Backwards Compatible Mode is used- Specified by:
isInBackwardsCompatibleMode
in interfaceStaticContext
- Returns:
- false; XPath 1.0 compatibility mode is not supported in the standalone XPath API
-
importSchema
Import a schema. This is possible only if the schema-aware version of Saxon is being used, and if the Configuration is a SchemaAwareConfiguration. Having imported a schema, the types defined in that schema become part of the static context.- Parameters:
source
- A Source object identifying the schema document to be loaded- Throws:
SchemaException
- if the schema contained in this document is invalidUnsupportedOperationException
- if the configuration is not schema-aware
-
isImportedSchema
Determine whether a Schema for a given target namespace has been imported. Note that the in-scope element declarations, attribute declarations and schema types are the types registered with the (schema-aware) configuration, provided that their namespace URI is registered in the static context as being an imported schema namespace. (A consequence of this is that within a Configuration, there can only be one schema for any given namespace, including the null namespace).- Specified by:
isImportedSchema
in interfaceStaticContext
- Returns:
- This implementation always returns false: the standalone XPath API does not support schema-aware processing.
-
getImportedSchemaNamespaces
Get the set of imported schemas- Specified by:
getImportedSchemaNamespaces
in interfaceStaticContext
- Returns:
- a Set, the set of URIs representing the names of imported schemas
-
isAllowedBuiltInType
Determine whether a built-in type is available in this context. This method caters for differences between host languages as to which set of types are built in.- Specified by:
isAllowedBuiltInType
in interfaceStaticContext
- Parameters:
type
- the supposedly built-in type. This will always be a type in the XS or XDT namespace.- Returns:
- true if this type can be used in this static context
-