Skip to content

Loading Dependent XSDs in Oracle through Python

March 31st, 2014 - Software

One essential feature is that schemas can refer to other schemas. Oracle supports it via the schemaURL parameter. In other words, the url of the referred schema must be the same as the schemaURL parameter used to register the referred schema.

One consequence of this feature is the necessity to provide a load order of schemas. In my case, The order would look like this:

schema_order = [
    'xocs-ani512-xml.xsd',
    'xocs-ani512-ani-ce.xsd',
    'xocs-ani512-ce.xsd',
    'xocs-ani512-cto.xsd',
    'xocs-ani512-ait.xsd',
    'xocs-ani512.xsd',
]

The loader segment would be something similar to:

for xsd_file in schema_order:
    xsd_path = os.path.join(args.dir, xsd_file)

    # DEBUG
    #
    print "Insert [%s] schema" % xsd_file
    xsd_content = open(xsd_path).read()

    insert_str = INSERT_STR % \
        {
            'name': xsd_file,
            'content': xsd_content,
        }

    cursor.execute(
        insert_str
    )
    connection.commit()

This is all trivial, except the INSERT_STR. A simple sample would be:

INSERT_STR = """
dbms_xmlschema.registerSchema(
    schemaURL => '%(name)s',
    schemaDoc => %(content)s,
    local => true,
    genTypes => false,
    genTables => true,
    enableHierarchy => dbms_xmlschema.ENABLE_HIERARCHY_NONE
)
"""

Note: Please refer to the previous article to see e.g. connection setup via cx_oracle.