Many language models work well with XML inputs, but XML can be a bit clunky to work with manually. Therefore, toolslm includes a couple of more streamlined approaches for XML generation.
An XML node contains a tag, optional children, and optional attributes. ft creates a tuple of these three things, which we will use to general XML shortly. Attributes are passed as kwargs; since these might conflict with reserved words in Python, you can optionally add a _ prefix and it’ll be stripped off.
ft('x-custom', ['hi'], _class='bar')
('x-custom', ['hi'], {'class': 'bar'})
Claudette has functions defined for some common HTML elements to create ft tuples more easily, including these:
from toolslm.xml import div,img,h1,h2,p,hr,html
a = html([ p('This is a paragraph'), hr(), img(src='http://example.prg'), div([ h1('This is a header'), h2('This is a sub-header', style='k:v'), ], _class='foo')])a
('html',
[('p', 'This is a paragraph', {}),
('hr', None, {}),
('img', None, {'src': 'http://example.prg'}),
('div',
[('h1', 'This is a header', {}),
('h2', 'This is a sub-header', {'style': 'k:v'})],
{'class': 'foo'})],
{})
To convert a tuple data structure created with ft and friends into XML, use to_xml, adding the hl parameter to optionally add syntax highlighting:
to_xml(a, hl=True)
<html> <p>This is a paragraph</p> <hr /> <img src="http://example.prg" /> <div class="foo"> <h1>This is a header</h1> <h2 style="k:v">This is a sub-header</h2> </div></html>
JSON doesn’t map as nicely to XML as the ft data structure, but for simple XML trees it can be convenient. The json_to_xml function handles that conversion:
a =dict(surname='Howard', firstnames=['Jeremy','Peter'], address=dict(state='Queensland',country='Australia'))print(json_to_xml(a, 'person'))