pyxb
pyxb copied to clipboard
Preserve XSD annotations in bindings
When preparing my XSD for PyXB binding, i would love to have access to the annotations of the type / attribute /element i am at in the XSD so i can inspect the bindings later on (while parsing the XML instance document) to see the annotations.
I would like i.e. to define an XSD annotation which states an attribute as a reference to another element so i can iterate over the instance document to resolve the reference later.
So in XSD i would like to write:
<xs:complexType name="TVariable">
<xs:attribute name="name" use="required"/>
<xs:attribute name="default"/>
<xs:attribute name="type" use="required">
<xs:annotation>
<xs:appinfo>
<dma:annotations>
<xsdTypeRef name="TTypeDefinition"/>
</dma:annotations>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:group name="TPacketContent">
<xs:sequence>
<xs:element name="var" minOccurs="0" maxOccurs="unbounded" type="TVariable"/>
</xs:sequence>
</xs:group>
And in my code i would like to write:
import myxsd
doc=myxsd.CreateFromLocation(...)
for var in doc.var:
if var._type.annotation.appinfo ... :
# look for source type
# add attribute to var:
var.type_resolved=findInDoc(doc,appinfo.xsdTypeRef())
(Roughly draftet so you hopefully get the idea).
The purpose of this is to annotate the schema so i can create a post-processing step over the instance XML document which looks at the annotations from the XSD to perform project-specific tasks like resolving references in the XML document or add specific consistency checks.
The reason to put it into the XSD is to have a single place where the datamodel is defined (XSD) and have a generic postprocessing based on PyXB and annotations which works for all data models.
Currently i am able to work around this by misusing the doucmentation field:
from build import msgdb
doc=msgdb.CreateFromDocument(open("MessageDB-Example.xml").read())
print("Gives type doc:")
print(doc.__doc__)
print("gives type name:")
print(doc._element().typeDefinition()._ExpandedName.localName())
So i need to parse the type doc to look out for my "annotation" and handle that.