python/genshi How to produce dynamic XML elements

708 views Asked by At

I am using Genshi directly in Python to generate XML using a template that contains most of the XML stuff. However, some XML elements in the template are supposed to get child elements that are dynamically generated by the Python program.

These are the parameters I use for Genshi rendering (in Python):

stream.render(method='xml', encoding="utf-8", out=outfile_fp, doctype=None)

Snippet from the Genshi template:

<mrp:Description py:if="description != None">
    <mrp:XHTMLWithLinks>${description}</mrp:XHTMLWithLinks>
</mrp:Description>

Sample value (in Python):

description = "<p>foo&bar</p>"

In reality, the values are more complex and are valid XHTML. Their structure is not predictible from a perspective of the template.

Genshi renders this to the following output:

<mrp:Description>
    <mrp:XHTMLWithLinks>&lt;p&gt;foo&amp;bar&lt;/p&gt;</mrp:XHTMLWithLinks>
</mrp:Description>

So it XML-escapes the special XML characters.

On the one hand, I have some sympathy for it doing that, on the other hand, it is not clear to me how I can solve my problem.

Strangely enough, I found some answers here that take XML as a variable value and seem to indicate that Genshi did not XML-escape them, for example here and here.

Andy

1

There are 1 answers

0
Pankrat On BEST ANSWER

You should be able to wrap the description string in a Markup instance. Something like

from genshi.core import Markup 
description = Markup(description)