Q:

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?

A:

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

