cybertools/xml/README.txt
helmutm 589238c76f created cybertools.xml package
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1477 fd906abe-77d9-0310-91a1-e0d9ade77398
2006-11-06 20:46:40 +00:00

94 lines
2.1 KiB
Text

XML (and XHTML) Generation
==========================
The elements generator lets you easily create snippets of XML or XHTML:
>>> from cybertools.xml.element import elements as e
>>> doc = e.html(
... e.head(e.title(u'Page Title')),
... e.body(
... e.div(u'The top bar', class_=u'top'),
... e.div(u'The body stuff', class_=u'body'),
... ))
>>> print doc.render()
<html>
<head>
<title>
Page Title
</title>
</head>
<body>
<div class="top">
The top bar
</div>
<div class="body">
The body stuff
</div>
</body>
</html>
An XML element thus created may be converted to an ElementTree:
>>> doc.renderTree()
'<html><head><title>Page Title</title></head><body><div class="top">The top bar</div><div class="body">The body stuff</div></body></html>'
>>> tree = doc.makeTree()
>>> tree.findtext('head/title')
'Page Title'
>>> xml = ('<html><head><title>Page Title</title></head><body>'
... '<div class="top">The top bar</div>'
... '<div class="body">The body stuff</div></body></html>')
>>> from cybertools.xml.element import fromXML
>>> doc = fromXML(xml)
>>> print doc.render()
<html>
<head>
<title>
Page Title
</title>
</head>
<body>
<div class="top">
The top bar
</div>
<div class="body">
The body stuff
</div>
</body>
</html>
Alternative Notation
--------------------
We can also create such a structure by successively adding elements
just by accessing an element's attributes:
>>> doc = e.html
>>> dummy = doc.head.title(u'Page Title')
>>> body = doc.body
>>> div1 = body.div(u'The top bar', class_=u'top')
>>> div2 = body.div(u'The body stuff', class_=u'body')
>>> print doc.render()
<html>
<head>
<title>
Page Title
</title>
</head>
<body>
<div class="top">
The top bar
</div>
<div class="body">
The body stuff
</div>
</body>
</html>
>>> for text in (u'Welcome', u'home'):
... p = div2.p(text, style='font-size: 80%;')
>>> print doc.render()
<html>...<p...>...</p>...