This module contains utils for XML encoding, decoding and printing.
XML decoding schemas can be defined following the general rules for schemas defined in the Module eoxserver.core.util.decoders. The XML decoder expects xml_location for the location parameter and xml_type for the type definition parameter. That means, XML decoding schemas generally have the form:
PARAM_SCHEMA = {
"<parameter_name>": {
"xml_location": "<xpath_expr>",
"xml_type": "<type_definition>"
},
...
}
where
EOxServer only supports a small subset of XPath expressions, see the class documentation of XPath below. Relative XPath expressions are interpreted to be refer to the document root element. The valid base type names for XML decoders are:
Minimum and maximum occurrences can be defined as described for the Module eoxserver.core.util.decoders and will be validated.
This class provides XML Decoding facilities.
The constructor accepts two optional arguments:
This method accepts one mandatory parameter params which is expected to be a string containing well-formed XML.
The input params is parsed into a DOM structure using Python’s xml.dom.minidom module.
This method accepts an XML decoding schema as described under XML Decoding Schemas above and sets the internal schema to this value.
Internally, the schema is parsed into a node structure. InternalError is raised in case the schema does not validate.
This method accepts an expression expr and a default value default as input.
If no schema has been defined, expr will be interpreted as an XPath expression. The string value of the element text is returned; if the value is missing default is returned.
If a schema has been defined, expr will be looked up in the schema, and the according value will be returned. If it is not found, default will be returned.
This method raises a XMLNodeOccurrenceError if the minimum or maximum occurrence bounds for the given XML element are violated. In case the text value of the XML element or attribute could not be casted to the expected type XMLTypeError is raised. In case expr is not defined in the schema InternalError is raised.
This method accepts an expression expr as input.
If no schema has been defined, expr will be interpreted as an XPath expression. The string value of the element text is returned; if the value is missing XMLNodeNotFound will be raised.
If a schema has been defined, expr will be looked up in the schema, and the according value will be returned. If it is not found, XMLNodeNotFound will be raised.
This method raises a XMLNodeOccurrenceError if the minimum or maximum occurrence bounds for the given XML element are violated. In case the text value of the XML element or attribute could not be casted to the expected type XMLTypeError is raised. In case expr is not defined in the schema InternalError is raised.
Returns the input XML.
Returns "xml".
This class represents an XPath expression. It provides methods for decoding an encoding XPath expressions as well as looking up the specified nodes in an XML structure.
The constructor accepts either an XPath expression or a list of locators as generated by XPathExprToList() as input.
Note that this implementation supports only a small subset of XPath expressions, namely parts of the abbreviated notation.
xpath_expr ::= "/" | ["/"] locator_list
locator_list ::= (element_locator "/")* node_locator
node_locator ::= element_locator | attribute_locator
element_locator ::= locator | "*"
attribute_locator ::= "@" locator | "@*"
locator ::= prefix ":" localname | ["{" namespaceuri "}"] localname
prefix ::= NCName
localname ::= NCName
namespaceuri ::= URI | "*"
This method converts an XPath expression to a list of locators.
Append another XPath expression and return the result.
Returns "element" if the XPath expression points to an element, or "attribute" if it points to an attribute.
Return all the nodes designated by the XPath expression.
Returns True if the XPath expression is absolute, False otherwise.
This method converts a list of locators to an XPath expression.
Generate an absolute XPath expression for the given node from the DOM.
This is the base class for XML encoders. It is intended to be subclassed by concrete encoder implementations which can use its utility methods to compose XML documents.
This method must be overridden by descendants in order to initialize the namespace dictionary of the object. The dictionary keys are interpreted as namespace prefixes whereas the values contain the namespace URIs.
The return value is the namespace dictionary.
This method creates elements. It expects three arguments as input:
If the content is
Node definition lists contain tuples that describe the elements or attributes to be created and/or to be appended to the parent element. 3-tuples of (prefix, tag_name, content) will be interpreted in the same way as the input parameters.
If the prefix or tag_name parameters start with a @ an attribute will be created and appended to the parent element. If the prefix or tag_name parameters contain @@ a text node will be created.
The content parameter can contain a node definition list as well.
Alternatively, 1-tuples containing a DOM Element can be specified. The DOM Element will be appended to the respective parent element.
This function takes a DOM element as input and returns an XML document with the input element as root encoded as ISO-8859-1 string. This function is namespace aware.
The optional nsmap parameter may contain a dictionary of XML prefixes and namespace URIs; these namespace definitions will be appended to the document root elements list of xmlns attributes. In case it is missing, the namespaces used throughout the document are automatically determined and the corresponding xmlns attributes will be created.
Takes a DOM document as input and returns the corresponding XML (no pretty printing) encoded as ISO-8859-1 string. This function is namespace aware.
The optional nsmap parameter may contain a dictionary of XML prefixes and namespace URIs; these namespace definitions will be appended to the document root elements list of xmlns attributes. In case it is missing, the namespaces used throughout the document are automatically determined and the corresponding xmlns attributes will be created.