XML

Concise generation of XML.
from IPython.display import Markdown
from pprint import pprint

source

XT

 XT (iterable=())

*Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.*


source

xt

 xt (tag:str, *c, **kw)

Create an XML tag structure [tag,children,attrs] for toxml()

The main HTML tags are exported as xt partials.

Attributes are passed as keywords. Use ‘klass’ and ‘fr’ instead of ‘class’ and ‘for’, to avoid Python reserved word clashes.

samp = Html(
    Head(Title('Some page')),
    Body(Div(P('Some text'), Input(name='me'), Img(src="filename"), klass='myclass'))
)
pprint(samp)
['html',
 (['head', (['title', ('Some page',), {}],), {}],
  ['body',
   (['div',
     (['p', ('Some text',), {}],
      ['input', (), {'name': 'me'}],
      ['img', (), {'src': 'filename'}]),
     {'class': 'myclass'}],),
   {}]),
 {}]

source

to_xml

 to_xml (elm, lvl=0)

Convert xt element tree into an XML string

print(to_xml(samp))
<html>
  <head>
    <title>
Some page
    </title>
  </head>
  <body>
    <div class="myclass">
      <p>
Some text
      </p>
      <input name="me">
      <img src="filename">
    </div>
  </body>
</html>

Automatic syntax highlighted output in notebooks:

samp
<html>
  <head>
    <title>
Some page
    </title>
  </head>
  <body>
    <div class="myclass">
      <p>
Some text
      </p>
      <input name="me">
      <img src="filename">
    </div>
  </body>
</html>