Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy external CSS and JavaScript in XSLT

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)?

like image 439
GrGr Avatar asked Jun 24 '09 12:06

GrGr


People also ask

Can I use JavaScript in XSLT?

JavaScript can run XSLT transformations through the XSLTProcessor object.

Can you use CSS with XSLT?

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.

What is the difference between CSS and XSLT?

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.

What is XSL in CSS?

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).


1 Answers

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()"/>
like image 121
Paul Sweatte Avatar answered Oct 23 '22 11:10

Paul Sweatte