PyXPathProcessor

class PyXPathProcessor

Bases: object

A PyXPathProcessor represents a factory to compile, load and execute XPath expressions.

base_uri

Get the static base URI for XPath expressions compiled using this XPathCompiler.

The base URI is part of the static context, and is used to resolve any relative URIs appearing within an XPath expression, for example a relative URI passed as an argument to the fn:doc() function. If no static base URI is supplied, then the current working directory is used.

Returns:

the base output URI as a string representation of the URI

Return type:

str

clear_parameters()

Clear all parameters set on the processor

clear_properties()

Clear all properties set on the processor

cwd

Property represents the current working directory

declare_namespace(prefix, uri)

Declare a namespace binding as part of the static context for XPath expressions compiled using this processor

Parameters:
  • prefix (str) – The namespace prefix. If the value is a zero-length string, this method sets the default namespace for elements and types.

  • uri (uri) – The namespace URI. It is possible to specify a zero-length string to “undeclare” a namespace; in this case the prefix will not be available for use, except in the case where the prefix is also a zero length string, in which case the absence of a prefix implies that the name is in no namespace.

declare_variable(name)

Declare a variable as part of the static context for XPath expressions compiled using this processor.

It is an error for the XPath expression to refer to a variable unless it has been declared. This method declares the existence of the variable, but it does not bind any value to the variable; that is done later, when the XPath expression is evaluated. The variable is allowed to have any type (that is, the required type is item()*).

Parameters:

name (str) – The name of the variable, as a string in clark notation

effective_boolean_value(xpath_str, encoding=None)

Evaluate the XPath expression, returning the effective boolean value of the result.

Parameters:
  • xpath_str (str) – XPath expression supplied as a string

  • encoding (str) – The encoding of the XPath string. Argument can be omitted or None. If not specified then the platform default encoding is used.

Returns:

The result is a boolean value.

Return type:

bool

Raises:

PySaxonApiError – Error if failure to evaluate XPath expression

evaluate(xpath_str, encoding=None)

Evaluate an XPath expression supplied as a string

Parameters:
  • xpath_str (str) – The XPath expression supplied as a string

  • encoding (str) – The encoding of the XPath string. Argument can be omitted or None. If not specified then the platform default encoding is used.

Returns:

the result of evaluating the XPath expression, or None if the expression returns an empty sequence.

Return type:

PyXdmValue

Raises:

PySaxonApiError – Error if failure to evaluate XPath expression

evaluate_single(xpath_str, encoding=None)

Evaluate an XPath expression supplied as a string, returning a single item

Parameters:
  • xpath_str (str) – The XPath expression supplied as a string

  • encoding (str) – The encoding of the XPath string. Argument can be omitted or None. If not specified then the platform default encoding is used.

Returns:

The first item of the returned sequence, or None if the expression returns an empty sequence.

Return type:

PyXdmItem

Note

A single XDM item is returned.

If the expression returns a sequence of more than one item, any items after the first are ignored.

Raises:

PySaxonApiError – Error if failure to evaluate XPath expression

get_property(name)

Get a property value set on the processor specified by name

Parameters:

name (str) – the name of the property

Returns:

value of the property

Return type:

str

import_schema_namespace(uri)

Import a schema namespace

Parameters:

uri (str) – The schema namespace to be imported. To import declarations in a no-namespace schema, supply a zero-length string.

is_schema_aware()
remove_parameter(name)

Remove the parameter given by name from the PyXPathProcessor. The parameter will not have any effect on the XPath if it has not yet been executed.

Parameters:

name (str) – The name of the XPath parameter

Returns:

True if the removal of the parameter has been successful, False otherwise.

Return type:

bool

set_backwards_compatible(option)

Say whether XPath 1.0 backwards compatibility mode is to be used

Parameters:

option (bool) – true if XPath 1.0 backwards compatibility is to be enabled, false if it is to be disabled.

set_base_uri(base_uri)

Set the static base URI for XPath expressions compiled using this XPathCompiler.

The base URI is part of the static context, and is used to resolve any relative URIs appearing within an XPath expression, for example a relative URI passed as an argument to the doc() function. If no static base URI is supplied, then the current working directory is used.

Parameters:

base_uri (str) – the base output URI

set_caching(is_caching)

Say whether the compiler should maintain a cache of compiled expressions.

Parameters:

is_caching (bool) – if set to true, caching of compiled expressions is enabled. If set to false, any existing cache is cleared, and future compiled expressions will not be cached until caching is re-enabled. The cache is also cleared (but without disabling future caching) if any method is called that changes the static context for compiling expressions, for example declare_variable() or declare_namespace().

set_context(**kwds)

Set the initial context for the XPath expression

Keyword Arguments:
  • file_name (str) – Path to the document to use as the initial context.

  • xdm_item (PyXdmItem) – The initial context item.

Only one of file_name or xdm_item should be provided.

set_cwd(cwd)

Set the current working directory

Parameters:

cwd (str) – current working directory

set_language_version(value)

Say whether an XPath 2.0, XPath 3.0, XPath 3.1 or XPath 4.0 processor is required.

Parameters:

value (str) – One of the values '1.0', '2.0', '3.0', '3.05', '3.1', '4.0'.

set_parameter(name, value)

Set the value of an XPath parameter

Parameters:
  • name (str) – the name of the XPath parameter, as a string. For a namespaced parameter use Clark notation '{uri}local'

  • value (PyXdmValue) – the value of the query parameter, or None to clear a previously set value

set_property(name, value)

Set a property specific to the processor in use.

Parameters:
  • name (str) – The name of the property

  • value (str) – The value of the property

Example

PyXPathProcessor: set serialization properties (names start with ‘!’ i.e. name “!method” -> “xml”) ‘resources’: directory to find Saxon data files, ‘s’: source as file name, ‘extc’: Register native library to be used with extension functions

set_schema_aware(schema_aware)
set_unprefixed_element_matching_policy(policy)

Set the policy for matching unprefixed element names in XPath expressions. Possible int values: DEFAULT_NAMESPACE = 0, ANY_NAMESPACE = 1 or DEFAULT_NAMESPACE_OR_NONE = 2

Parameters:

policy (int) – The policy to be used

use_schema(schema: PyXsdSchema)