Solved

Disable output escaping

Posted on 2007-11-27
1
892 Views
Last Modified: 2013-11-18
Hello Expert [Gertone],

I have an same query which i asked before about disable output escaping.
In output htmls i have found that at some places it is retaining &lt; as input xml also contains &lt; but at few places it is not retaing and changed it to '<'...I have also unit test and check the unit test output is proper.

Please find below test.xml and test.xsl for the same...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xalan="http://xml.apache.org/xalan">
      <xsl:variable name="hiddenProductName" select="'flex'"/>
      <!--xsl:character-map name="disable">
            <xsl:output-character character="&amp;" string="&amp;"/>
            <xsl:output-character character="&lt;" string="&lt;"/>
            <xsl:output-character character="&gt;" string="&gt;"/>
      </xsl:character-map-->
      <xsl:template match="/">
            <xsl:apply-templates/>
      </xsl:template>
      <xsl:template match="p">
            <xsl:variable name="product" select="./@product|./@class | ./@outputclass"/>
            <xsl:if test="string-length($product)=0 or not($product=concat($hiddenProductName,'only'))">
                  <xsl:value-of disable-output-escaping="yes" select="'&lt;p&gt;'"/>
                  <xsl:call-template name="processTags"/>
                  <xsl:value-of disable-output-escaping="yes" select="'&lt;/p&gt;'"/>
            </xsl:if>
      </xsl:template>
      <xsl:template name="processTags">
            <xsl:param name="input"/>
            <xsl:param name="addParagraphTags" select="false()"/>
            <xsl:param name="createLinkFromRootContext" select="false()"/>
            <xsl:variable name="matchHTML" select="./adobetable |  ./ol |  ./p | ./codeblock |  ./ul |  ./b |  ./adobeimage | ./ph |  ./codeph | ./bold | ./strong |  ./em |  ./i |  ./xref | ./pre | ./text() | ./li| ./sup"/>
            <xsl:for-each select="$matchHTML">
                  <xsl:if test="self::text()">
                        <xsl:if test="$addParagraphTags">
                              <xsl:if test="position()=1">
                                    <xsl:value-of disable-output-escaping="yes" select="'&lt;p&gt;'"/>
                              </xsl:if>
                        </xsl:if>
                        <xsl:variable name="text">
                              <xsl:call-template name="search-and-replace">
                                    <xsl:with-param name="search-string" select="'~~'"/>
                                    <xsl:with-param name="replace-string" select="'*'"/>
                                    <xsl:with-param name="input" select="."/>
                              </xsl:call-template>
                        </xsl:variable>

                        <xsl:value-of select="$text" disable-output-escaping="no"/>

                        <xsl:if test="$addParagraphTags">
                              <xsl:if test="position()=last()">
                                    <xsl:value-of disable-output-escaping="yes" select="'&lt;/p&gt;'"/>
                              </xsl:if>
                        </xsl:if>
                  </xsl:if>
                  <xsl:if test="self::codeblock">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::ol | self::ul">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::li">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::p">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::b | self::bold | self::strong">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::adobetable">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::adobeimage">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::ph">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::sup">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::em | self::i">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::codeph">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
                  <xsl:if test="self::xref">
                        <xsl:apply-templates select=".">
                              <xsl:with-param name="createLinkFromRootContext" select="$createLinkFromRootContext"/>
                        </xsl:apply-templates>
                  </xsl:if>
                  <xsl:if test="self::pre">
                        <xsl:apply-templates select="."/>
                  </xsl:if>
            </xsl:for-each>
      </xsl:template>
      <xsl:template match="pre">
            <xsl:variable name="product" select="@product| @class | @outputclass"/>
            <xsl:if test="string-length($product)=0 or not($product=concat($hiddenProductName,'only'))">
                  <xsl:value-of disable-output-escaping="yes" select="'&lt;pre&gt;'"/>
                  <!-- Bug#:1583029 -->
                  <xsl:variable name="replacelt">
                        <xsl:call-template name="search-and-replace">
                              <xsl:with-param name="search-string" select="'&lt;'"/>
                              <xsl:with-param name="replace-string" select="'&amp;lt;'"/>
                              <xsl:with-param name="input" select="."/>
                        </xsl:call-template>
                  </xsl:variable>
                  <xsl:variable name="texts">
                        <xsl:call-template name="search-and-replace">
                              <xsl:with-param name="search-string" select="'&gt;'"/>
                              <xsl:with-param name="replace-string" select="'&amp;gt;'"/>
                              <xsl:with-param name="input" select="$replacelt"/>
                        </xsl:call-template>
                  </xsl:variable>
                  <xsl:call-template name="processTags">
                        <xsl:with-param name="input" select="$texts"/>
                  </xsl:call-template>
                  <xsl:value-of disable-output-escaping="yes" select="'&lt;/pre&gt;'"/>
            </xsl:if>
      </xsl:template>
      <xsl:template name="search-and-replace">
            <xsl:param name="input"/>
            <xsl:param name="search-string"/>
            <xsl:param name="replace-string"/>
            <xsl:choose>
                  <xsl:when test="$search-string and contains($input,$search-string)">
                        <xsl:value-of select="substring-before($input,$search-string)"/>
                        <xsl:value-of select="$replace-string" disable-output-escaping="no"/>
                        <xsl:call-template name="search-and-replace">
                              <xsl:with-param name="input" select="substring-after($input,$search-string)"/>
                              <xsl:with-param name="search-string" select="$search-string"/>
                              <xsl:with-param name="replace-string" select="$replace-string"/>
                        </xsl:call-template>
                  </xsl:when>
                  <xsl:otherwise>
                        <xsl:value-of select="$input" disable-output-escaping="no"/>
                  </xsl:otherwise>
            </xsl:choose>
      </xsl:template>
</xsl:stylesheet>


<?xml version="1.0" encoding="UTF-8"?>
<root>
    <p class="- topic/p ">To set the value of this property using CSS:
        <pre xml:space="preserve" class="- topic/pre ">
            PlotSeries {
            fills:#CC66FF, #9966CC, #9999CC;
            }
        </pre>
    </p>
    <p class="- topic/p ">To set the value of this property using MXML:
        <pre xml:space="preserve" class="- topic/pre ">
            &lt;mx:PlotSeries ... &gt;
              &lt;mx:fills&gt;
                &lt;mx:SolidColor color="0xCC66FF"/&gt;
                &lt;mx:SolidColor color="0x9966CC"/&gt;
                &lt;mx:SolidColor color="0x9999CC"/&gt;
              &lt;/mx:fills&gt;
            &lt;/mx:PlotSeries&gt;
        </pre>
    </p>
</root>

can you suggest what's going wrong or any other alternative for this?

Thanks,
Shailesh
0
Comment
Question by:ShaileshShinde
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
Comment Utility
The search and replace template is there to make sure that a
&lt; is replcaed by a &amp;lt;
resulting in the output you are receiving

this would work in this case as a template for <pre>

    <xsl:template match="pre">
        <xsl:variable name="product" select="@product| @class | @outputclass"/>
        <xsl:if test="string-length($product)=0 or not($product=concat($hiddenProductName,'only'))">
            <pre>
            <xsl:value-of disable-output-escaping="yes" select="."/>
            </pre>
        </xsl:if>
    </xsl:template>

cheers

Geert
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now