I have an XSL transformation which outputs HTML. In the head
element I have a CSS file reference.
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
I would like to create a standalone HTML result without external references and thus I would like to include external CSS references. To prevent code duplication, I do not want to hard code the styles into the XSLT template, so I am looking for some XSLT command to copy the file contents of the CSS file. I know xsl:include
or xsl:import
won't work, since they expect XSLT files. Neither does
<xsl:copy-of select="document('css/styles.css')"/>
as it expects something XML compliant.
I also have some JavaScript function declarations which I would like to copy as well.
Is this possible with pure XSLT, or will I have to do some pre-processing of the XSLT file (or post-processing of the HTML file)?
I have an XSL transformation which outputs HTML. In the head
element I have a CSS file reference.
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
I would like to create a standalone HTML result without external references and thus I would like to include external CSS references. To prevent code duplication, I do not want to hard code the styles into the XSLT template, so I am looking for some XSLT command to copy the file contents of the CSS file. I know xsl:include
or xsl:import
won't work, since they expect XSLT files. Neither does
<xsl:copy-of select="document('css/styles.css')"/>
as it expects something XML compliant.
I also have some JavaScript function declarations which I would like to copy as well.
Is this possible with pure XSLT, or will I have to do some pre-processing of the XSLT file (or post-processing of the HTML file)?
Share Improve this question edited Mar 7, 2014 at 20:06 Paul Sweatte 24.6k7 gold badges131 silver badges268 bronze badges asked Jun 24, 2009 at 12:38 GrGrGrGr 4,1985 gold badges23 silver badges20 bronze badges3 Answers
Reset to default 13XSLT 2.0 provides the unparsed-text() function to read documents via URL that are not XML.
In XSLT 1.0, if you don't need to be too script about the CSS, you can use the following to make the CSS file XML-compatible. And, fortunately, the browsers tolerate the HTML comments.
CSS
<!--/*--><root><![CDATA[<!--*/-->
body
{
margin: 0;
}
div > p
{
background-color: yellow;
}
<!--/*-->]]></root><!--*/-->
XSLT
<style type="text/css">
<xsl:value-of select="document('test.css')" disable-output-escaping="yes" />
</style>
Use a processing instruction to wrap the CSS content:
<?xml version="1.0" encoding="utf-8"?>
<root>
<?wrapper html
<html>
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
</html>
?>
</root>
Then tweak the existing xsl:copy-of
select statement to render it:
<xsl:copy-of select="document('css/styles.css')//processing-instruction()"/>
Maybe you could trick it into thinking the stylesheet is XML.
styles.css
/*
<?xml version="1.0" encoding="utf-8"?>
<style>
<![CDATA[
*/
... styles ...
/*
]]>
</style>
*/
It's a hack but if there is no other way it might suffice (assuming it works at all).