Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • IAMMRBONGO 29 posts 49 karma points
    May 19, 2011 @ 16:54
    IAMMRBONGO
    0

    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):

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:umbraco.contour="urn:umbraco.contour"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour ">


    <xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8"/>

    <xsl:param name="currentPage"/>
    <xsl:variable name="thumbWidth" select="number(90)" />
    <xsl:variable name="thumbHeight" select="number(90)" />
    <xsl:variable name="maxWidth" select="number(600)" />

    <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)"/>&amp;width=<xsl:value-of select="$thumbWidth"/>&amp;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>  
      
      



    </PictureLibrary>
    </xsl:template>

    </xsl:stylesheet>
  • IAMMRBONGO 29 posts 49 karma points
    May 19, 2011 @ 17:11
    IAMMRBONGO
    0

    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:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>

     

  • IAMMRBONGO 29 posts 49 karma points
    May 19, 2011 @ 17:20
    IAMMRBONGO
    0

    Got rid of the leading space. It was in the template file. Still need to work out where the extra line is from though

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 19, 2011 @ 21:32
    Chriztian Steinmeier
    1

    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:

    <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server"><% Response.ContentType = "text/xml"; %><?xml version="1.0"?><umbraco:macro runat="server" ...>

    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

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 19, 2011 @ 23:50
    Jan Skovgaard
    1

    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

  • IAMMRBONGO 29 posts 49 karma points
    May 20, 2011 @ 12:04
    IAMMRBONGO
    0

    I'd high five you but I don't have the karma points. Someone give these guys a high five

Please Sign in or register to post replies

Write your reply to:

Draft