encode special character in rendering template into XML

59 views
Skip to first unread message

can xiang

unread,
Nov 17, 2006, 10:46:38 PM11/17/06
to django...@googlegroups.com
I'm trying to render template into XML output. If the str representation
of the variable contains special character of XML, it would cause
trouble. The same thing would happen if trying to render into CSV or
other format.

It would be nice if the render function of Template can take additional
parameter to selectively encode the string according the XML entity
reference or other spec. It means a patch of "encode_output" function of
"VariableNode" may needed.

Is this solution reasonable or I missed something?

James Bennett

unread,
Nov 17, 2006, 11:40:59 PM11/17/06
to django...@googlegroups.com
On 11/17/06, can xiang <xian...@gmail.com> wrote:
> Is this solution reasonable or I missed something?

In the case of XML, the general best practice -- and not just in
Django -- is not to use templates, but rather tho use tools designed
for the creation and manipulation of XML and which understand and obey
the necessary constraints.

--
"May the forces of evil become confused on the way to your house."
-- George Carlin

can.xiang

unread,
Nov 17, 2006, 11:58:31 PM11/17/06
to Django users
Thanks for your suggestion. I was thinking about generate XML in views
directly.

But the XML file I want is just simple one like HTML(they are also
small) and it's all about presentation, thus it doesn't make sense to
put them into views.

Any other way to do it?

Russell Keith-Magee

unread,
Nov 18, 2006, 12:52:32 AM11/18/06
to django...@googlegroups.com
On 11/18/06, can xiang <xian...@gmail.com> wrote:
>
> I'm trying to render template into XML output. If the str representation
> of the variable contains special character of XML, it would cause
> trouble. The same thing would happen if trying to render into CSV or
> other format.

The current solution would be to use a filter; for example, there is
an 'escape' filter to escape html special characters. You would need
to write a similar filter for the XML special character set.

> It would be nice if the render function of Template can take additional
> parameter to selectively encode the string according the XML entity
> reference or other spec. It means a patch of "encode_output" function of
> "VariableNode" may needed.

Interesting idea; however, there is at least one problem. How do you
handle occasions where you want the raw, unencoded value of a variable
node? In light of this, I'm not sure that the idea you are proposing
is significantly more elegant than manually escaping variables using a
filter.

Yours,
Russ Magee %-)

Fredrik Lundh

unread,
Nov 18, 2006, 1:57:57 AM11/18/06
to django...@googlegroups.com
can.xiang wrote:

I'm of course slightly biased, but I always use ElementTree for things
like this, with a trivial filter like the one described here:

http://online.effbot.org/2006_02_01_archive.htm#20060203

</F>

can.xiang

unread,
Nov 18, 2006, 10:09:57 AM11/18/06
to Django users
Russ, thanks for your suggestion.

Because all I want is a pure XML output, so for this case, I want all
variable encoded at one time, for non-variable string I can deal with
it manually.

After some experiment on attaching encoding function TextNode and
VarialbeNode (actually it's the first time I view the code and I don't
know if I put my hand in the right place), I found it wouldn't have a
easy patch because it's not easy to separate the raw string and
variable.

Fredrik, thanks for your idea and snippet. If I have to put xml
generating code in views I would follow you. It would be easier to use
JSON output than XML, if presentation data have to be generated in
views rather in template.

I really hope django could render XML and CSV with template as it
claims though.

Fredrik Lundh

unread,
Nov 18, 2006, 11:34:58 AM11/18/06
to django...@googlegroups.com
can.xiang wrote:

> Fredrik, thanks for your idea and snippet. If I have to put xml
> generating code in views I would follow you.

it works just fine for (X)HTML fragments too, of course.

(...and is especially useful of you're creating the context variables
on the fly, using searches/transforms on an initial model loaded by the
view, for example).

</F>

can.xiang

unread,
Nov 18, 2006, 9:22:54 PM11/18/06
to Django users

"Fredrik Lundh 写道:
"

> (...and is especially useful of you're creating the context variables
> on the fly, using searches/transforms on an initial model loaded by the
> view, for example).
>
> </F>

Really?! can you show me some pointer or example on how to do it? I
hate to prepare all presentational data in views(initial model is ok )
and I need to render context variable on the fly.

In short, for your example {{ body_elem|tostring}}, how to get
"body_elem" except in the view? Of course I don't want to apply this
filter for each of the template variable too.

Reply all
Reply to author
Forward
0 new messages