Using ZPT to create XML - early processing
I've had good success generating XML using Page Templates, but just ran into a problem I can't figure out. I'm trying to create an RSS 2.0 feed, and wanted to be able to fill in the time dynamically in the comment at the top of the page. Here's an example of the first three lines of the page template: <?xml version="1.0" ?> <!-- RSS generated by Zope 2.6.1 on Sat, 15 Mar 2003 13:29:11 EST --> <rss version="2.0" xmlns:tal="http://xml.zope.org/namespaces/tal" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"> If I try to use TAL in the comment line, it doesn't get processed. It seems that ZPT will not process TAL content within the comment. I've also tried to generate the whole comment tag from TAL using something like: <dummy tal:replace="python:'<!-- foo -->'">foo</dummy> - or - <dummy tal:replace="structure python:'<!-- foo -->'">foo</dummy> - or - <dummy tal:replace="structure python:'<' + '!' + '-- foo -->'">foo</dummy> but they all result in an error when I try to save it: Compilation failed xml.parsers.expat.ExpatError: not well-formed (invalid token): line 17, column 35 So is there any way to generate commented XML using TAL? ___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://opentech.leafe.com
Ed Leafe wrote:
<?xml version="1.0" ?> <!-- RSS generated by Zope 2.6.1 on Sat, 15 Mar 2003 13:29:11 EST --> [...] If I try to use TAL in the comment line, it doesn't get processed. It seems that ZPT will not process TAL content within the comment. [...] So is there any way to generate commented XML using TAL?
This works: <tal:comment xmlns:tal="http://xml.zope.org/namespaces/tal" define="now python:DateTime().strftime('%e %b %Y')" replace="structure string:<!-- $now -->" /> TAL doesn't process comments, so you do have to generate the entire comment. The XML parser doesn't like unescaped '<>&' characters in attribute values, so you need to escape them. Finally, when generating XML you need to make sure there's a 'xmlns:tal' declaration on or around your tag -- since there's no surrounding tag in this case, we have to put it on the tag itself. Cheers, Evan @ 4-am
On Sunday, March 16, 2003, at 11:36 AM, Evan Simpson wrote:
Ed Leafe wrote:
<?xml version="1.0" ?> <!-- RSS generated by Zope 2.6.1 on Sat, 15 Mar 2003 13:29:11 EST --> [...] If I try to use TAL in the comment line, it doesn't get processed. It seems that ZPT will not process TAL content within the comment. [...] So is there any way to generate commented XML using TAL?
This works:
<tal:comment xmlns:tal="http://xml.zope.org/namespaces/tal" define="now python:DateTime().strftime('%e %b %Y')" replace="structure string:<!-- $now -->" />
TAL doesn't process comments, so you do have to generate the entire comment. The XML parser doesn't like unescaped '<>&' characters in attribute values, so you need to escape them. Finally, when generating XML you need to make sure there's a 'xmlns:tal' declaration on or around your tag -- since there's no surrounding tag in this case, we have to put it on the tag itself.
That makes sense, but it still doesn't seem to work when it's located at the very top of the XML document, before the root element. I have the <?xml version="1.0" ?> line, followed by your suggestion, followed by the root element, and I get the following error: Compilation failed xml.parsers.expat.ExpatError: junk after document element: line 8, column 0 Line 8 is the line containing the root element. If I place that exact same line *after* the root element, there is no problem. ___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://opentech.leafe.com
participants (2)
-
Ed Leafe -
Evan Simpson