Craig Johnstone
Craig Johnstone

Reputation: 303

Use XSLT to convert HTML to escaped HTML

I have a large HTML page which is stored within an XSLT variable like so:

<xsl:variable name="html">
  <html>
    <head>
      <title>Test page</title>
    </head>
    <body>
      <div class="section">
        <h1>Title</h1>
        <p>Here is some content.</p>
      </div>
    </body>
  </html>
</xsl:variable>

I want to be able to use XSLT to output the contents of this variable as:

  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Test page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div class=&quot;section&quot;&gt;
        &lt;h1&gt;Title&lt;/h1&gt;
        &lt;p&gt;Here is some content.&lt;/p&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;

Can someone point me in the right direction to structure my XSLT in order to achieve this?

I want to do something like this, but this isn't valid, obviously:

<xsl:value-of select="replace($html,'< >"','&lt; &gt;')" />

Any suggestions?

Upvotes: 2

Views: 2756

Answers (2)

Mads Hansen
Mads Hansen

Reputation: 66781

Using Evan Lenz stylesheet xml-to-string.xsl will achieve what you want.

The following stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="xml-to-string.xsl"/>

    <xsl:variable name="html">
        <html>
            <head>
                <title>Test page</title>
            </head>
            <body>
                <div class="section">
                    <h1>Title</h1>
                    <p>Here is some content.</p>
                </div>
            </body>
        </html>
    </xsl:variable>

    <xsl:template match="/">
        <xsl:call-template name="xml-to-string">
            <xsl:with-param name="node-set" 
                           select="document('')/*/xsl:variable[@name='html']/*"/>
        </xsl:call-template>
    </xsl:template>

</xsl:stylesheet>

produces the following output:

&lt;html&gt;
            &lt;head&gt;
                &lt;title&gt;Test page&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div class="section"&gt;
                    &lt;h1&gt;Title&lt;/h1&gt;
                    &lt;p&gt;Here is some content.&lt;/p&gt;
                &lt;/div&gt;
            &lt;/body&gt;
        &lt;/html&gt

Upvotes: 2

helderdarocha
helderdarocha

Reputation: 23627

Since you have that content in a variable inside your XSLT file, I assume you can change it. In that case you can simply enclose the code inside a CDATA section:

<xsl:variable name="html"><![CDATA[
    <html>
        <head>
            <title>Test page</title>
        </head>
        <body>
            <div class="section">
                <h1>Title</h1>
                <p>Here is some content.</p>
            </div>
        </body>
    </html>
]]></xsl:variable>

That will cause the entire block to be treated as a string (instead of being treated as XML and converted to its string value). Now you can use it and print it anywhere as a string:

<xsl:template match="/">
    <body>
       <h1>Here is some HTML Code</h1>
       <pre>
           <xsl:value-of select="$html"/>
       </pre>
    </body>
</xsl:template>

This is the result of the transformation:

<body>
    <h1>Here is some HTML Code</h1>
    <pre>
        &lt;html&gt;
            &lt;head&gt;
                &lt;title&gt;Test page&lt;/title&gt;
            &lt;/head&gt;
            &lt;body&gt;
                &lt;div class="section"&gt;
                    &lt;h1&gt;Title&lt;/h1&gt;
                    &lt;p&gt;Here is some content.&lt;/p&gt;
                &lt;/div&gt;
            &lt;/body&gt;
        &lt;/html&gt;
    </pre>
</body>

Upvotes: 2

Related Questions