Hi all, Me yet again. I was wondering if anyone knew how to get umbraco to set the encoding when generating an XML file. I'm trying to output a feed from a template which calls it's own alternative template to provide it XML for an ajax feed (It's a picture library and I only want to pre-load a certain number of images at a time similar to flickr).
I've got to get the xml file formatted as I want it using XML (it needs to be lightweight and follow a specific structure). What I've got is an alt template which I can in AJAX.
The problem I have is that I keep setting the XML:output encoding to utf-8 which is then ignored by the output. Code below (Any ideas, I never quite get encoding right):
<xsl:template match="/"> <PictureLibrary> <xsl:for-each select="$currentPage/GalleryImage"> <xsl:variable name="mediaFolderId" select="$currentPage/mediaLibraryID" /> <!-- Set a variable to the image ID To see if it exists in the media library --> <xsl:variable name="imageId" select="galleryImage"/> <!-- Set a second variable to the image caption --> <xsl:variable name="caption" select="galleryCaption"/> <!-- Loop through the media library to find the image --> <xsl:for-each select="umbraco.library:GetMedia($mediaFolderId, true())/Image"> <!-- check that the umbraco file was found -->
<xsl:if test="umbracoFile != ''"> <!-- Match the 2 for loops on image ID --> <xsl:if test="@id = $imageId"> <xsl:element name="pic"> <xsl:attribute name="title"><xsl:value-of select="$caption"/></xsl:attribute> <xsl:attribute name="tPAdd"><![CDATA[/imageGen.ashx?image=]]><xsl:value-of select="umbraco.library:UrlEncode(umbracoFile)"/>&width=<xsl:value-of select="$thumbWidth"/>&height=<xsl:value-of select="$thumbHeight"/></xsl:attribute> <xsl:attribute name="lPAdd"><![CDATA[/imageGen.ashx?image=]]><xsl:value-of select="umbraco.library:UrlEncode(umbracoFile)"/><![CDATA[&width=]]><xsl:value-of select="$maxWidth"/><![CDATA[&Constrain=True]]></xsl:attribute> <xsl:attribute name="fPAdd"><xsl:value-of select="umbraco.library:UrlEncode(umbracoFile)"/></xsl:attribute> </xsl:element> </xsl:if> </xsl:if> </xsl:for-each> </xsl:for-each>
It looks like the main problem may be that it places a single whitespace character before the xml declaration and also a blank line. I have no idea where they are coming through and the encoding is not registering as UTF-8.
I tried removing the following but it made no difference:
You should set omit-xml-declaration to "yes" and silently ignore the output encoding issue - here's why:
- The way the transformation is carried out, results in always getting the output XMLString as UTF-16; that's just how it works in the .NET classes that handle that.
In the template you should not have any linebreaks inside the <asp:Content> tag, before the <umbraco:macro> tag - something like this:
Note that we set the ContentType to "text/xml"; the XML declaration isn't mandatory, but if you leave it in, don't specify the encoding - it really should be handled automatically...
This always works for me - BUT: The other half of doing encoding right with XML, is to actually save files (that includes XSLT files) in the encoding they say they are (which is almost always UTF-8).
If you don't want to use the ASP.NET stuff for setting the ContentType you could use the "ChangeContentType" extension in your xslt file instead. Like this
Encoding in XSLT
Hi all,
Me yet again. I was wondering if anyone knew how to get umbraco to set the encoding when generating an XML file. I'm trying to output a feed from a template which calls it's own alternative template to provide it XML for an ajax feed (It's a picture library and I only want to pre-load a certain number of images at a time similar to flickr).
I've got to get the xml file formatted as I want it using XML (it needs to be lightweight and follow a specific structure). What I've got is an alt template which I can in AJAX.
The problem I have is that I keep setting the XML:output encoding to utf-8 which is then ignored by the output. Code below (Any ideas, I never quite get encoding right):
It looks like the main problem may be that it places a single whitespace character before the xml declaration and also a blank line. I have no idea where they are coming through and the encoding is not registering as UTF-8.
I tried removing the following but it made no difference:
Got rid of the leading space. It was in the template file. Still need to work out where the extra line is from though
Hi IAMMRBONGO,
You should set omit-xml-declaration to "yes" and silently ignore the output encoding issue - here's why:
- The way the transformation is carried out, results in always getting the output XMLString as UTF-16; that's just how it works in the .NET classes that handle that.
In the template you should not have any linebreaks inside the <asp:Content> tag, before the <umbraco:macro> tag - something like this:
Note that we set the ContentType to "text/xml"; the XML declaration isn't mandatory, but if you leave it in, don't specify the encoding - it really should be handled automatically...
This always works for me - BUT: The other half of doing encoding right with XML, is to actually save files (that includes XSLT files) in the encoding they say they are (which is almost always UTF-8).
/Chriztian
Hi IAMMRBONGO (and Chriztian)
I just want to add a tip...
If you don't want to use the ASP.NET stuff for setting the ContentType you could use the "ChangeContentType" extension in your xslt file instead. Like this
<xsl:value-of select="umbraco.library:ChangeContentType('text/xml')"/>
You can place it anywhere in your <xsl:template match="/"></xsl:template>
/Jan
I'd high five you but I don't have the karma points. Someone give these guys a high five
is working on a reply...