Reputation: 303
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:
<html>
<head>
<title>Test page</title>
</head>
<body>
<div class="section">
<h1>Title</h1>
<p>Here is some content.</p>
</div>
</body>
</html>
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,'< >"','< >')" />
Any suggestions?
Upvotes: 2
Views: 2756
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:
<html>
<head>
<title>Test page</title>
</head>
<body>
<div class="section">
<h1>Title</h1>
<p>Here is some content.</p>
</div>
</body>
</html>
Upvotes: 2
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>
<html>
<head>
<title>Test page</title>
</head>
<body>
<div class="section">
<h1>Title</h1>
<p>Here is some content.</p>
</div>
</body>
</html>
</pre>
</body>
Upvotes: 2