GenerateDS

Create Python Classes from a LIXI2 Schema

As a Python developer, the first thing you will want to do when you start working with the LIXI schema is generate Python classes from the XSD file. To save you some time (and get you on the real work as quickly as possible) here is a quick guide!

The Python library we will be using to generate the Python classes is called GenerateDS.

generateDS.py generates Python data structures (for example, class definitions) from an XML Schema document. These data structures represent the elements in an XML document described by the XML Schema. It also generates parsers that load an XML document into those data structures. In addition, a separate file containing subclasses (stubs) is optionally generated. The user can add methods to the subclasses in order to process the contents of an XML document.
- generateDS Project Description

The generateDS package can be installed with this command:

pip install generateDS

From the command line, you can now execute the command to generate your LIXI Python Classes. Use the following command, making sure to replace the path with the path to your LIXI2 schema:

generateDS.py -f -o CAL.py -s CALsubs.py --super=CAL C:\Users\john\LIXI-CAL-2_6_23.xsd

Two files will be produced and should look something like this:

CAL.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-#
# Generated Mon Jan  6 15:19:57 2020 by generateDS.py version 2.35.8.
# Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
#
# Command line options:
#   ('-f', '')
#   ('-o', 'CAL.py')
#   ('-s', 'CALsubs.py')
#   ('--super', 'CAL')
#
# Command line arguments:
#   C:\Users\john\Desktop\Repository\Schemas\LIXI-CAL-2_6_23.xsd
#
# Command line:
#   C:\Users\john\AppData\Local\Programs\Python\Python36-32\Scripts\generateDS.py -f -o "CAL.py" -s "CALsubs.py" --super="CAL" C:\Users\john\Desktop\Repository\Schemas\LIXI-CAL-2_6_23.xsd
#
# Current working directory (os.getcwd()):
#   project
#
 
from six.moves import zip_longest
import os
import sys
import re as re_import base64
 
...

CALsubs.py

#!/usr/bin/env python
#
# Generated Mon Jan  6 15:20:03 2020 by generateDS.py version 2.35.8.
# Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
#
# Command line options:
#   ('-f', '')
#   ('-o', 'CAL.py')
#   ('-s', 'CALsubs.py')
#   ('--super', 'CAL')
#
# Command line arguments:
#   C:\Users\john\Desktop\Repository\Schemas\LIXI-CAL-2_6_23.xsd
#
# Command line:
#   C:\Users\john\AppData\Local\Programs\Python\Python36-32\Scripts\generateDS.py -f -o "CAL.py" -s "CALsubs.py" --super="CAL" C:\Users\john\Desktop\Repository\Schemas\LIXI-CAL-2_6_23.xsd
#
# Current working directory (os.getcwd()):
#   project
#
 
import os
import sys
from lxml import etree as etree_
 
...

How to use the Python Classes you have Generated

To use the Python classes you have generated, create a new Python file in your project and import your Schema classes. You can now create your LIXI package element object. Here is a simple example creating a LIXI2 package containing only the publisher name and contact email:

import CAL

package = CAL.Package()
publisher = CAL.PublisherType()
package.set_Publisher(publisher)
publisher.set_CompanyName("LIXI")
publisher.set_ContactName("John Matthews")
publisher.set_Email("john@lixi.org.au")

Related Posts...

To learn more about LIXI tools for Python developers try our post The Python LIXI Package.

We've published similar blog posts showing how to generate C# classes here and JAVA Classes here.


Written by:
John Matthews, LIXI Technical Lead
First Published: February 5, 2020