dtd2xsc – Creating XIST namespaces from DTDs
Purpose
dtd2xsc is a script that helps create XIST namespace modules from DTDs. It reads one or more DTDs and outputs a skeleton namespace module.
Options
dtd2xsc supports the following options:
- -x <name>, --xmlns <name>
The default namespace name. All elements that don’t belong to any namespace will be assigned to this namespace.
- -s <value>, --shareattrs <value>
Should attributes be shared among the elements?
nonemeans that each element will have its own standaloneAttrsclass directly derived fromll.xist.Elements.Attrs. Fordupeseach attribute that is used by more than one element will be moved into its ownAttrsclass. Forallthis will be done for all attributes.
- -m <value>, --model <value>
Add model information to the namespace.
nodoesn’t add any model information.simpleonly addsmodel = Falseormodel = True(i.e. only the information whether the element must be empty or not).fullalladds all.xist.simsmodel object to each element class.fullonceadds full model information to, but reuses model objects for elements which have the same model.
- -d <flag>, --defaults <flag>
Should default values for attributes specified in the DTD be added to the XIST namespace (as the
defaultspecification in the attribute class)? (Allowed values arefalse,no,0,true,yesor1)
- --duplicates <value>
If more that one DTD is specified on the command line, some elements might be specified in more than one DTD.
--duplicatesspecifies how to handle this case:rejectdoesn’t allow multiple element specifications.allowallows them, but only if both specifications are identical (i.e. have the same attributes).mergeallows them and adds the attribute specification of all element specifications to the resulting XIST namespace.
Note that dtd2xsc requires lxml to work.
Example
Suppose we have the following DTD file (named foo.dtd):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!ELEMENT persons (person*)>
<!ELEMENT person (firstname?, lastname?)>
<!ATTLIST person id CDATA #REQUIRED>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
Then we can generate a skeleton XIST namespace from it with the following command:
$ dtd2xsc ~/gurk.dtd -xhttp://xmlns.example.org/ -mfullall
The output will be:
# -*- coding: ascii -*-
from ll.xist import xsc, sims
xmlns = 'http://xmlns.example.org/'
class firstname(xsc.Element): xmlns = xmlns
class lastname(xsc.Element): xmlns = xmlns
class person(xsc.Element):
xmlns = xmlns
class Attrs(xsc.Element.Attrs):
class id(xsc.TextAttr): required = True
class persons(xsc.Element): xmlns = xmlns
person.model = sims.Elements(lastname, firstname)
persons.model = sims.Elements(person)
firstname.model = sims.NoElements()
lastname.model = sims.NoElements()