Derek Broughton wrote:
Martin Aspeli wrote:

David Glick wrote:
Derek Broughton wrote:
I have a couple of browser views that are trying to render XML and
getting bitten by the "bug" of
https://bugs.launchpad.net/zope2/+bug/142801

I realize this isn't really a bug, but how DOES one set a mime-type on a
browser view?  There doesn't seem to be anything in the ZCML<view>
definition.  Because the ISO 19139 xml tags I'm trying to use are mixed
case, but the template is being processed as text/html, the mixed-case
tags below are being rendered in lowercase - and then my xsl stylesheet
doesn't
work.  If I put the<?xml-stylesheet?>   tag into the viewlet, it would
probably actually look like it worked, but the raw XML would still be
wrong, since those tags really are mixed-case.

My view template is simply:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <?xml-stylesheet type="text/xsl" href="++resource++ISO2text.xsl" ?>
      <div tal:replace="structure provider:MetadataViewlets"
        xmlns:tal="http://xml.zope.org/namespaces/tal";
      />

while the code included by the viewlet manager starts:

    <mcp:MD_Metadata
        xmlns:ns1="http://www.opengis.net/gml/";
        xmlns:tal="http://xml.zope.org/namespaces/tal";
        xmlns:xlink="http://www.w3.org/1999/xlink";
        xmlns:mcp="http://bluenet3.antcrc.utas.edu.au/mcp";
        xmlns:gco="http://www.isotc211.org/2005/gco";
        xmlns:gmd="http://www.isotc211.org/2005/gmd";
        xmlns:srv="http://www.isotc211.org/2005/srv";
        xmlns:gts="http://www.isotc211.org/2005/gts";
gco:isoType="gmd:MD_Metadata">
      <gmd:fileIdentifier>
          <gco:CharacterString tal:content="context/id" />
      </gmd:fileIdentifier>
...

You probably need to set the Content-Type HTTP header in the
response...here's an example from

http://svn.plone.org/svn/plone/Plone/branches/3.3/Products/CMFPlone/skins/plone_templates/rss_template.pt
--

<metal:block tal:define="dummy
python:request.RESPONSE.setHeader('Content-Type',
'text/xml;;charset='+context.plone_utils.getSiteEncoding())" />
Instead of doing this in the template, I'd do it in the view itself. e.g.:

class MyView(object):
      def __init__(self, context, request):
          self.context = context
          self.request = request
      def __call__(self):
          self.request.response.setHeader('Content-Type',
'text/xml;;charset="utf-8"')
          return self.index() # render template associated in ZCML

(You can look up the site encoding if you'd like, but it's going to be
utf-8 in virtually all situations. After speaking to Hanno about this
I've made the utf-8 assumption explicit in my code most of the time.)

That makes more sense than setting it in the template - at least we have a
content-type before the TAL markup is rendered.  Unfortunately, it doesn't
work either.  I have tried:
- setting the View name to "Text.xml"

That's kind of irrelevant. Zope sets the Content-Type header.

- setting an attribute "content_type = 'text/xml'" in the view class

Don't know why you think that would work. Nothing I know of would look for it. :)

- the examples from you and David

My example definitely works. You can see it in action in plone.namedfile's @@download view for example.

- your example omitting the second, I believe extraneous, ';' in
   'text/xml;;charset="utf-8"' and/or omitting the __init__() (that's just
   the default method, is it not?)
all with no success.


Why don't you try without the charset first, just to test a simpler header value?

Is it possible to explicitly invoke the TAL XML parser in the "self.index()"
call above, rather than the HTML parser?  I guess it must be...

I'm sure it is, but that's not what controls the Content-Type header that is sent back to the browser. That header is set by setBody() in ZPublisher.HTTPResponse::

        isHTML = self.isHTML(self.body)
        if not self.headers.has_key('content-type'):
            if isHTML:
                c = 'text/html; charset=%s' % default_encoding
            else:
                c = 'text/plain; charset=%s' % default_encoding
            self.setHeader('content-type', c)
        else:
            c = self.headers['content-type']
            if c.startswith('text/') and not 'charset=' in  c:
c = '%s; charset=%s' % (c, default_encoding)
                self.setHeader('content-type', c)

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book


_______________________________________________
Product-Developers mailing list
[email protected]
http://lists.plone.org/mailman/listinfo/product-developers

Reply via email to