Skip to content
This repository has been archived by the owner on Apr 29, 2023. It is now read-only.

Preserve XSD annotations in bindings #112

Open
hcw70 opened this issue Mar 26, 2019 · 1 comment
Open

Preserve XSD annotations in bindings #112

hcw70 opened this issue Mar 26, 2019 · 1 comment

Comments

@hcw70
Copy link

hcw70 commented Mar 26, 2019

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.

@hcw70
Copy link
Author

hcw70 commented Mar 26, 2019

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant