Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Disable output escaping

Posted on 2007-11-27
1
Medium Priority
?
919 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 750 total points
ID: 20364176
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

705 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