Another small 4Suite MarkupWriter example: XHTML 1.1

I was writing code to emit XHTML 1.1 using 4Suite and just to double-check the doc types I looked at the spec. I thought it might be useful to write up a small MarkupWriter example for emitting the example in the spec.

from Ft.Xml.MarkupWriter import MarkupWriter
from xml.dom import XHTML_NAMESPACE, XML_NAMESPACE

XHTML_NS = unicode(XHTML_NAMESPACE)
XML_NS = unicode(XML_NAMESPACE)
XHTML11_SYSID = u"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
XHTML11_PUBID = u"-//W3C//DTD XHTML 1.1//EN"

writer = MarkupWriter(indent=u"yes", doctypeSystem=XHTML11_SYSID,
                      doctypePublic=XHTML11_PUBID)
writer.startDocument()
writer.startElement(u'html', XHTML_NS, attributes={(u'xml:lang', XML_NS): u'en'})
writer.startElement(u'head', XHTML_NS)
writer.simpleElement(u'title', XHTML_NS, content=u'Virtual Library')
writer.endElement(u'head', XHTML_NS)
writer.startElement(u'body', XHTML_NS)
writer.startElement(u'p', XHTML_NS)
writer.text(u'Moved to ')
writer.simpleElement(u'a', XHTML_NS,
                     attributes={u'href': u'http://vlib.org/'},
                     content=u'vlib.org')
writer.text(u'.')
writer.endElement(u'p', XHTML_NS)
writer.endElement(u'body', XHTML_NS)
writer.endElement(u'html', XHTML_NS)
writer.endDocument()

It's worth mentioning that this example would be even simpler with template output facilities I've proposed for Amara.

[Uche Ogbuji]

via Copia
2 responses
You know, I can't get the line about ...attributes={(u'xml:lang', XML_NS): u'en'}) to run at all. I just downloaded and installed amara and 4Suite, and the closest I came was ...attributes={(u'xml:lang'): u'en'}), and even then the output omitted the "xml" from "xml:lang". Can you tell me what I might be doing wrong? I get  ...File "C:\Python23\lib\site-packages\Ft\Xml\MarkupWriter.py", line 85, in startElement

  value = attributes[name]

KeyError: u'xmllang'

when I try the version as listed above.



Thanks,

Ron
Sorry.  It turns out this code depends on a bug fix I made a month ago or so, but still hasn't made it into any full 4Suite release.  I recommend grabbing the latest MarkupWriter.py from cvsweb:



http://cvs.4suite.org/viewcvs/checkout/4Suite/Ft/Xml/MarkupWriter.py



And replacing the file on your disk with it.  This fix will be in the next 4Suite release, of course.



You can also just get 4Suite/CVS in its entirety:



http://4suite.org/docs/4SuiteCVS.xml



But just getting the above file should do the trick for your current complaint.



Good luck.