Selecting elements from an XSD schema to populate in a Flask app

399 Views Asked by At

Ultimately I would like to build a sample xml file based on an XSD schema in a Flask app, then export the input as a valid xml file.

I am using the xmlschema package, too.

I have the basic structure of a flask app and page with a dropdown menu displaying elements from XSD.

@app.route('/xml_builder')
def xml_builder():
    xs = xmlschema.XMLSchema('schema_file.xsd')
    dict = sorted(xs.maps.elements.keys())

    return render_template('public/xml-builder.html', get = dict)

However, I'm struggling to get / understand how to make it only display the elements from the main xsd (schema file.xsd as described later on) - when selecting that element from the dropdown menu it should then display the relevant sub-elements / attributes from the other xsd file (schema_types.xsd as described later) as labels with a text field next to it to allow the user to enter a value.

The schema is made up multiple schemas

schema_file.xsd is the main schema and describes the structure of the xml file

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema
            attributeFormDefault="unqualified"
            elementFormDefault="qualified"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:include schemaLocation="schema_types.xsd" />

<xsd:complexType name="InfoData">
        <xsd:sequence>
            <xsd:element name="Shop"
                         type="test:ShopType"
                         maxOccurs="unbounded" />
            <xsd:element name="Town"
                         type="test:TownType"
                         minOccurs="0"
                         maxOccurs="unbounded" />
           </xsd:element>
        </xsd:sequence>
</xsd:complexType>
</xsd:schema>

and the schema_types.xsd file contains the structure for the element

<xsd:complexType name="ShopType">
        <xsd:sequence>
            <xsd:element name="Country"
                         type="xsd:string"
                         minOccurs="0">
            </xsd:element>
            <xsd:element name="City"
                         type="xsd:string"
                         minOccurs="0">
            </xsd:element>
            <xsd:element name="Note"
                         type="xsd:string"
                         minOccurs="0"
                         maxOccurs="unbounded">
            </xsd:element>
        </xsd:sequence>
        <xsd:attribute name="id"
                       type="xsd:string"
                       use="required">
        </xsd:attribute>
        <xsd:attribute name="name"
                       type="xsd:string"
                       use="required">
        </xsd:attribute>
    </xsd:complexType>

Any direction / guidance would be greatly appreciative

1

There are 1 best solutions below

0
On

The distinction between 'main schema' and 'other xsd file' is specific to your scenario. XML Schema makes no such distinction, and I expect the xmlschema Python module will not either.

If possible you should to declare a targetNamespace for the main schema, and a different targetNamespace for the sub-elements schema. You will also need to change your xs:include to xs:import. Then you can select elements based on their namespace.

Bear in mind that this solution will be very specific to your own schema. A general-purpose example document generator for XSDs is a big project.