beta.xquery

The xquery module provides a Python API for the XQuery 3.1 specification. This Python interface enables the user to compile an xquery expression and to execute it with different input data. The typical starting point would be the functions beta.xquery.Expression.compile() and beta.xquery.Expression.execute().

There are also utility functions beta.xquery.compile() and beta.xquery.execute() for simplified usage.

>>> from altova_api.v2.beta import xquery
>>> from altova_api.v2.beta.xpath import SerializationParams, Session, MapItem, Sequence, AtomicItem
>>> import altova_api.v2.xsd as xsd
>>>
>>> expr_str = '''
>>>     declare variable $iterations external := 5;
>>>     <out>{(1 to $iterations)!<child nr="{.}"/>}</out>
>>> '''
>>> s = Session()
>>> ext_vars = MapItem(s)
>>> ext_vars.set_value(AtomicItem.create_from_QName(:class:`xsd.QName`('iterations'), s), Sequence.create_from_item(AtomicItem.create_from_int(3, s)))
>>> res = :meth:`beta.xquery.execute`(
>>>     expr_str,
>>>     session=s,
>>>     external_variables=ext_vars,
>>>     delivery_format=DeliveryFormat.SERIALIZED,
>>>     default_serialization_params=SerializationParams(s, indent=False, omit_xml_declaration=True)
>>> )
>>> assert res.main_value[0].anySimpleType().value == '''<out><child nr="1"/><child nr="2"/><child nr="3"/></out>'''

Functions

beta.xquery.compile(expression, *, Session session, **kwargs)

Validate the provided expression and create a compiled expression from it. Internally uses the beta.xquery.Expression.compile() classmethod but has different return values. Returns an beta.xquery.Expression object on success or raises an exception if there are syntax or static errors detected. The keyword arguments corresponding to the properties of beta.xquery.CompileOptions are supported and are used to initialize the internally created options object.

beta.xquery.execute(expression, *, Session session, initial_context=None, **kwargs)

Compile and execute the xquery expression. Returns an beta.xpath.ResultList, or raises an exception if an error is encountered. The keyword arguments corresponding to the properties of beta.xquery.CompileOptions and beta.xquery.RuntimeOptions are supported and are used in the initialization of the respective objects.

>>> from altova_api.v2.beta import xquery
>>> from altova_api.v2.beta.xpath import SerializationParams, Session, MapItem, Sequence, AtomicItem, TypeConverter
>>> import altova_api.v2.xsd as xsd
>>>
>>> expr_str = 'fold-left((1 to ($max - 1) idiv 2), 2, function($primes, $val){$primes, (2*$val+1)[every $i in $primes satisfies . mod $i]})'
>>> s = Session()
>>> ext_vars = MapItem(s)
>>> ext_vars.set_value(AtomicItem.create_from_QName(:class:`xsd.QName`('max'), s), Sequence.create_from_item(AtomicItem.create_from_int(16, s)))
>>> res = :meth:`beta.xquery.execute`(
>>>      expr_str,
>>>      session=s,
>>>      external_variables=ext_vars,
>>>      allow_undeclared_variables=True
>>> )
>>> tc = TypeConverter()
>>> tc.to_python(res.main_value)
(2, 3, 5, 7, 11, 13)