[XML-SIG] A bit off topic: XML advice needed...

Laurent Szyster l.szyster@ibm.net
Thu, 04 Mar 1999 10:16:17 +0100


Stuart Hungerford wrote:
> 
> Now if I could just get a Python to XML translator
> I could do it all in Python... ;-)

Do you mean something like the Pickle module, but producing XML
instead of custom text format?

Since the XML documents should reflect the instance classe specific
constraint, such translator will need to produce both the DTD's and
the documents, for each 'root' element.

All instance members of type String, Int, Long, Float and None should
be translated as ATTRIBUTES while instances and collections (such as
tuple, dict and list) will be translated as ELEMENTS. The distinction
beeing based on whether the object can contain others or not.

To allow the translator to produce DTD _and_ reuse existing element
definitions, we may add "hidden" class members (starting with "_xml')
holding the element and attributes definition.

Like,

  class date:

    _xml_ELEMENT = ('#CDATA')
    _xml_ATTLIST = ('notation NOTATION', 'value IDREF #REQUIRED')

    notation = "iso8601-w3c"	# this is the default for the
                                # 'notation' attribute

    def __init__(self, value=None):
      self.value = value

    ...

  class event:

    _xml_ELEMENT = ('startdate', 'enddate')
    _xml_ATTLIST = ('name')

    name = "unknown"

    def __init__(self):
      self.name = 'noname'
      self.startdate = date()
      self.enddate = date()

    ...

Would result in the following document with its DTD included:

  <?xml version="1.0"?>
  <!DOCTYPE event
    [
    <!ELEMENT event (startdate, enddate)>
    <!ATTLIST event
              name CDATA "unknown"
              >
    <!ELEMENT date >
    <!ATTLIST date
              notation CDATA "unknown"
              value IDREF #REQUIRED
              >
    ]>

  ... here comes the document root ....


The nice thing with this design is that it may allow to add XML
pickling for existing classes, at a relatively low cost in terms of
work (because DTD's are generated automagically).

Any volunteers?
;-)


Laurent