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)?
JavaScript can run XSLT transformations through the XSLTProcessor object.
An XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.
Difference between XSLT and CSS transformationCSS is used for the user interface purpose and XSLT is a language where we can implement condition types. CSS changes the visual impact whereas XSLT is responsible for structural impact. CSS is supported by all the browsers while XSLT is not supported.
XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. This tutorial will teach you how to use XSLT to transform XML documents into other formats (like transforming XML into HTML).
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()"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With