Solved

Disable output escaping

Posted on 2007-11-27
1
895 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple PHP - search by location postcode/zip code 8 31
How to I "stripe" columns using XSLT 3 38
css help 3 109
recommended CMS for the website revamp 3 92
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 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

25 Experts available now in Live!

Get 1:1 Help Now