Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 23, 2011 @ 15:13
    Jeroen Breuer
    0

    Render CDATA with Xslt

    Hello,

    I tried to past a Remarketing code into the umbraco backend but I got an exception. Here is the code:

    <!-- Google Code for Remarketing List --> 
    <script type="text/javascript"> 
    /* <![CDATA[ */ 
    var google_conversion_id = 1071937084; 
    var google_conversion_language = "en"; 
    var google_conversion_format = "3"; 
    var google_conversion_color = "666666"; 
    var google_conversion_label = "test"; 
    var google_conversion_value = 0; 
    /* ]]> */ 
    </script> 
    <script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"> 
    </script> 
    <noscript> 
    <div style="display:inline;"> 
    <img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1071937084/?label=test&amp;guid=ON&amp;script=0"/> 
    </div> 
    </noscript>

    If I paste this into an umbraco textbox in the backend I get this exception:

    Cannot have ']]>' inside an XML CDATA block. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.ArgumentException: Cannot have ']]>' inside an XML CDATA block.
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
    
    Stack Trace: 
    
    
    [ArgumentException: Cannot have ']]>' inside an XML CDATA block.]
       System.Xml.XmlTextWriter.WriteCData(String text) +185
       System.Xml.XmlCDataSection.WriteTo(XmlWriter w) +25
       System.Xml.XmlElement.WriteContentTo(XmlWriter w) +32
       System.Xml.XmlElement.WriteTo(XmlWriter w) +157
       System.Xml.XmlElement.WriteContentTo(XmlWriter w) +32
       System.Xml.XmlElement.WriteTo(XmlWriter w) +157
       System.Xml.XmlNode.get_OuterXml() +111
       umbraco.cms.businesslogic.CMSNode.SavePreviewXml(XmlNode x, Guid versionId) +566
       umbraco.cms.businesslogic.web.Document.SaveXmlPreview(XmlDocument xd) +98
       umbraco.cms.businesslogic.web.Document.Save() +186
       umbraco.cms.presentation.editContent.Save(Object sender, EventArgs e) +2193
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       umbraco.controls.ContentControl.saveClick(Object Sender, ImageClickEventArgs e) +453
       umbraco.controls.ContentControl.savePublish(Object Sender, ImageClickEventArgs e) +48
       System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +115
       System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +120
       System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
    
    
    
    So I tried a workaround. I created 2 properties (remarketingCDATA and remarketingScript) and wanted to build the rest of the code in XSLT. This way the data can be stored in Umbraco and XSLT eventually would render the correct script. Here is my XSLT:
    <?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:ProjectXsltExtensions="urn:ProjectXsltExtensions" xmlns:uTube.XSLT="urn:uTube.XSLT" 
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ProjectXsltExtensions uTube.XSLT ">
    
    
    <xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
    
      <xsl:if test="$currentPage/remarketingCDATA != '' and $currentPage/remarketingScript != ''">
        <!-- Google Code for Remarketing List -->
        <script type="text/javascript">
          /* <![CDATA[ */ 
          <xsl:value-of select="$currentPage/remarketingCDATA" disable-output-escaping="yes"/>
          /* ]]> */
        </script>
        <script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
        </script>
        <noscript>
          <div style="display:inline;">
            <xsl:value-of select="$currentPage/remarketingScript" disable-output-escaping="yes"/>
          </div>
        </noscript>
      </xsl:if>
    
    </xsl:template>
    
    </xsl:stylesheet>
    Now I hoped this would render the code correctly, but it doesn't. Xslt comments some parts out because of the /* which is used. This is what is rendered:
    <script type="text/javascript">
        /*  */ 
        <xsl:value-of select="$currentPage/remarketingCDATA" disable-output-escaping="yes"/>
        /*  */
    </script>
    <script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script><noscript>
        <div style="display:inline;"><img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1071937084/?label=test&amp;guid=ON&amp;script=0"/> </div>
    </noscript>
    How can I make sure the CDATA are also rendered? Using xsl:text didn't help...
    Jeroen
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 23, 2011 @ 15:15
    Sebastiaan Janssen
    2

    Have a look at the examples here, you can concat the CDATA around it..

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 23, 2011 @ 15:45
    Jeroen Breuer
    1

    Thanks Sebastiaan!

    Here is my sample which works:

    <?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:ProjectXsltExtensions="urn:ProjectXsltExtensions" xmlns:uTube.XSLT="urn:uTube.XSLT" 
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ProjectXsltExtensions uTube.XSLT ">
    
    
    <xsl:output method="html" omit-xml-declaration="yes" indent="yes"/>
    
    <xsl:param name="currentPage"/>
    
    <xsl:template match="/">
    
      <xsl:if test="$currentPage/remarketingCDATA != '' and $currentPage/remarketingScript != ''">
        <!-- Google Code for Remarketing List -->
        <script type="text/javascript">
          /* &lt;![CDATA[ */
          <xsl:value-of select="$currentPage/remarketingCDATA" disable-output-escaping="yes"/>
          /* ]]&gt; */
        </script>
        <script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
        </script>
        <noscript>
          <div style="display:inline;">
            <xsl:value-of select="$currentPage/remarketingScript" disable-output-escaping="yes"/>
          </div>
        </noscript>
      </xsl:if>
    
    </xsl:template>
    
    </xsl:stylesheet>

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft