Register
Tuesday, February 07, 2012
 
 DBAs And ProgrammersBlog
  
News! Minimize
   
 
 Print   
 
Misc Blog Stuff Minimize
   
 
 Print   
 
The Reluctant DBA Minimize
 
 
 
 Print   
 
Reluctant DBA Minimize
   
 
  
 
Reluctant DBA Minimize
   
 
  
 
The Reluctant DBA Minimize
 
Aug12

Written by:CarpDeus
8/12/2008 1:30 PM 

The two most difficult problems in computer science are supposed to be Naming and Cache Invalidation. But, as my friend and co-worker Mike Amundsen and I have postulated, when it comes to web development the third difficult problem is escaped characters.

HTML is filled with instructions that are set off by < and >. So, if I wanted to indent a section of text:

Like this text is

I need to put it in a set of <blockquote> tags. But, in order for you to see the tag with the angle brackets and not have it interpreted as an instruction to indent tags by your browser, I need to put either &lt;blockquote&gt; or &#60;blockquote&#62;. Which both will show up as <blockquote>, because &lt; and &#60; are instructions to the browser to render the < symbol. Which sounds simple enough but the & is also an escaped character and for me to show you &lt; I really need to use &amp;#60;.

Confused? Add in XML which may contain something like "The telegraph giant, AT&amp;T" which may or may not need to be preserved and  you can get into some right, royal messes. So you can escape every & sign but if the & is in front of an amp; then you'll end up with output that looks like AT&amp;T when you wanted AT&T.

I mention all this because I was using an XSTL transform to take data and prepare it for SQL Server by wrapping the text in single quotes and doubling up any single quotes it encountered. And it was giving me invalid XML because it was stripping out the &amp; and replacing it with &. Once I asked Mike about it he told me about a simple little paramter that made it all work better. That transform, btw, is here:

<xsl:template name="sqlApostrophe">
    <xsl:param name="string"/>
    <xsl:variable name="apostrophe">'</xsl:variable>
    <xsl:choose>
        <xsl:when test="contains($string,$apostrophe)">
            <xsl:value-of select="concat(substring-before($string,$apostrophe),$apostrophe,$apostrophe)" disable-output-escaping="no"/>
            <xsl:call-template name="sqlApostrophe">
                <xsl:with-param name="string" select="substring-after($string,$apostrophe)"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string" disable-output-escaping="no"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

The key, in bold, is the disable-output-escaping. Set it to "no" and everything worked fine.

Thanks Mike!

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment  Cancel 
 
 
  
 
Privacy Statement | Terms Of Use Copyright 2001-2008 by ReluctantDBA.com