Skip to content
Snippets Groups Projects

XML Converter

Merged Alexander Schlemmer requested to merge f-xml-converter into dev
All threads resolved!
Compare and Show latest version
2 files
+ 46
24
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -39,7 +39,7 @@ from .stores import GeneralStore, RecordStore
from .structure_elements import (BooleanElement, DictElement, Directory, File,
FloatElement, IntegerElement, JSONFile,
ListElement, NoneElement, StructureElement,
TextElement, XMLTagElement, XMLTextNode)
TextElement, XMLTagElement, XMLTextNode, XMLAttributeNode)
from .utils import has_parent
import lxml.etree
@@ -93,6 +93,13 @@ class XMLTagConverter(Converter):
- When attribs_as_children is set to true, attribute nodes will be generated from the attributes
of the matched tags.
Namespaces
----------
The default is to take the namespace map from the current node and use it in xpath queries.
Because default namespaces cannot be handled by xpath, it is possible to remap the default namespace
using the key "default_namespace".
The key "nsmap" can be used to define additional nsmap entries.
"""
if not isinstance(element, XMLTagElement):
raise TypeError("Element must be an instance of XMLTagElement.")
@@ -106,6 +113,11 @@ class XMLTagConverter(Converter):
nsmap[default_namespace] = nsmap[None]
del nsmap[None]
# Set additional nsmap entries from the converter definition:
if "nsmap" in self.definition:
for key, value in self.definition["nsmap"].items():
nsmap[key] = value
xpath = self.definition.get("xpath", "child::*")
children = element.tag.xpath(xpath, namespaces=nsmap)
el_lst = []
@@ -113,7 +125,13 @@ class XMLTagConverter(Converter):
if isinstance(el, str):
raise RuntimeError("Only standard xml nodes are supported as results of xpath queries.")
elif isinstance(el, lxml.etree._Element):
el_lst.append(XMLTagElement(el))
if self.definition.get("tags_as_children", True):
el_lst.append(XMLTagElement(el))
if self.definition.get("attribs_as_children", False):
for attrib in el.attrib:
el_lst.append(XMLAttributeNode(el, attrib))
if self.definition.get("text_as_children", False):
el_lst.append(XMLTextNode(el))
else:
raise RuntimeError("Unsupported child type.")
return el_lst
Loading