Elementtree and CDATA handling

uche.ogbuji at gmail.com uche.ogbuji at gmail.com
Sun Jun 5 05:59:41 CEST 2005


"If, instead, you want to keep track of where the CDATA sections are,
and output them again without change, you'll need to use an
XML-handling interface that supports this feature. Typically, DOM
implementations do - the default Python minidom does, as does pxdom.
DOM is a more comprehensive but less friendly/Python-like interface for
XML processing. "

Amara in CVS makes it easy to perform the output part of this:

text="""<html><head>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
//<![CDATA[
function matchwo(a,b)
{
if (a < b && a > 0) then
   {
   return 1
   }
}

//]]>
</script>
</body>
</html>
"""

from amara.binderytools import bind_string
doc = bind_string(text)
print doc.xml(cdataSectionElements=[u'script'])

Output:

<?xml version="1.0" encoding="UTF-8"?>
<html><head>
<title>Document</title>
</head>
<body>
<script type="text/javascript"><![CDATA[
//
function matchwo(a,b)
{
if (a < b && a > 0) then
   {
   return 1
   }
}

//
]]></script>
</body>
</html>

Unfortunately, in cooking up this example I did find a bug in the Amara
1.0b1 release that requires a workaround.  I should be releasing 1.0b2
this weekend, which fixes this bug (among other fixes and
improvements).

"If you're generating output for legacy browsers, you might want to
just
use a 'real' HTML serialiser. "

Amara does provide for this, e.g.:

from amara.binderytools import bind_string
doc = bind_string(text)
print doc.xml(method=u"html")

Which automatically and transparently brings to bear the full power of
the XSLT HTML output method.

--
Uche Ogbuji                               Fourthought, Inc.
http://uche.ogbuji.net                    http://fourthought.com
http://copia.ogbuji.net                   http://4Suite.org
Use CSS to display XML, part 2 -
http://www-128.ibm.com/developerworks/edu/x-dw-x-xmlcss2-i.html
XML Output with 4Suite & Amara -
http://www.xml.com/pub/a/2005/04/20/py-xml.htmlUse XSLT to prepare XML
for import into OpenOffice Calc -
http://www.ibm.com/developerworks/xml/library/x-oocalc/
Schema standardization for top-down semantic transparency -
http://www-128.ibm.com/developerworks/xml/library/x-think31.html




More information about the Python-list mailing list