Render escaped XML in browser

For the following xml, I want to render the unanswered parts of the text in the browser from xslt, for example:

<one>
&lt;text&gt;
</one>

I want the markup displayed in the browser to be:

&lt;text&gt;

But when I use apply patterns that look below

<xsl:template match="text()" mode="literalHTML">
    <xsl:copy-of select=".">
    </xsl:copy-of>
</xsl:template>

The above XML gets rendered as:

<text>

How to change this template so that it prints & lt; text & gt; in the browser?

Best regards, Keshav

+2
source share
3 answers

This can be achieved in XSLT 1.0 using fairly complex recursive processing.

, FXSL ( XSLT) :

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:testmap="testmap"
exclude-result-prefixes="xsl f testmap">
   <xsl:import href="str-dvc-map.xsl"/>

   <testmap:testmap/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>

   <xsl:template match="/">
     <xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vTestMap"/>
       <xsl:with-param name="pStr" select="/*/text()"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template name="escape" mode="f:FXSL"
     match="*[namespace-uri() = 'testmap']">
      <xsl:param name="arg1"/>

      <xsl:choose>
       <xsl:when test="$arg1 = '&lt;'">&amp;lt;</xsl:when>
       <xsl:when test="$arg1 = '&gt;'">&amp;gt;</xsl:when>
       <xsl:otherwise><xsl:value-of select="$arg1"/></xsl:otherwise>
      </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

, XML-:

<one>
&lt;text&gt;
</one>

:

&amp;lt;text&amp;gt;

, : &lt;text&gt;

+2

" "

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()" name="text">
        <xsl:param name="text" select="."/>
        <xsl:if test="$text != ''">
            <xsl:variable name="first" select="substring($text,1,1)"/>
            <xsl:choose>
                <xsl:when test="$first = '&lt;'">&amp;lt;</xsl:when>
                <xsl:when test="$first = '&gt;'">&amp;gt;</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$first"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:call-template name="text">
                <xsl:with-param name="text" select="substring($text,2,(string-length($text)-1) div 2 + 1)"/>
            </xsl:call-template>
            <xsl:call-template name="text">
                <xsl:with-param name="text" select="substring($text,(string-length($text)-1) div 2 + 3)"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

:

<one>
&amp;lt;text&amp;gt;
</one>

EDIT: DVC, .

+2

I also wrote a solution using recursion before, but the only thing that bothered me was performance, and if there would be some memory or some stackoverflow because of this. I was wondering if there would be any solution using <xsl: value-of> or <copy-of>? Also, please let me know if there are any improvements in the solution below if recursion can be converted to a loop, etc.

<xsl:template match="text()" mode="literalHTML">
    <xsl:variable name="txt" select="."/>
    <xsl:value-of select="smc:escapeChar(smc:escapeChar(smc:escapeChar($txt,'&amp;','&amp;amp;'),'&lt;','&amp;lt;'),'&gt;','&amp;gt;')"/>
</xsl:template>
<xsl:function name="smc:escapeChar">
    <xsl:param name="txt"/>
    <xsl:param name="char"/>
    <xsl:param name="subs"/>
    <xsl:result>
        <xsl:variable name="result">
            <xsl:choose>
                <xsl:when test="contains($txt, $char)">
                    <xsl:variable name="after" select="substring-after($txt,$char)"/>
                    <xsl:value-of select="substring-before($txt,$char)"/>
                    <xsl:value-of select="$subs"/>
                    <xsl:value-of select="smc:escapeChar($after,$char,$subs)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$txt"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:value-of select="$result"></xsl:value-of>
        </xsl:result>
</xsl:function>
0
source

Source: https://habr.com/ru/post/1758216/


All Articles