Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transform XML to HTML w/ XSLT with CSS included (not linked) 1

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
Is there a way to include the CSS within the target HTML during an XSL transformation?

I know how to do the transformation... I would just like to inject the css code into the generated HTML document...

This started out as an Excel file, which I saved to an HTML file, which created over 60 classes in a CSS section...

I seperated the Style code to a seperate CSS file to unclutter the HTML...

I then Cleaned up the HTML code (man... Excel is messy ;-))

Then I modified the HTML to an XSLT doc

I am then using VB with MSXML DOM to grab node sets from several XML files and place clones of them into a newly created DOMDocument object... Which I am then using the XSLT on to produce an HTML doc...

I realise I can just cut and paste the CSS code back into the XSLT doc, but I would rather keep it seperate if possible...
Otherwise, it will add about 300 lines of crap to the top of the XSLT doc...
 
... just include the css file as you would in the HTML file..

eg

Code:
<xsl:template name="stylesheet" ...>
<LINK href="/buzz/scripts/ems.css" rel="stylesheet" />
</xsl:template>

or,

Code:
<xsl:template name="stylesheet" ..>
<style type="text/css"> 
  .petit {
  	font-size: 8pt;	color: #999999;	font-family: Arial, Verdana, Helvetica;  	
  }
  </style>
</xsl:template>


Unfortunately you cannot use xsl:import or xsl:include to include the stylesheet straight from the css file; xsl will try to parse the document and fail. However if you did want to put the stylesheet in a separate xsl document and in its own named template you could then import it from the main document like this:

Code:
<!-- included xsl template called "csstemplate.xsl"-->
<xsl:template  name="css-style">
<style type="text/css"> 

  .petit {
  	font-size: 8pt;	color: #999999;	font-family: Arial, Verdana, Helvetica;  	
  }
  </style>
</xsl:template>

and then import/include it like this:

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:import href="./csstemplate.xsl"/>

<!-- rest of xsl script -->

</xsl:stylesheet>
..and then just call the template in the normal way from within the main xsl stylesheet (with <xsl:call-template>).

hope that helps some.

matt
 
I didn't think about including the CSS in a second XSL template... that should work nicely to include the CSS for external use...

Also... since I am using vb... I guess I could use a replace function on the output text before saving the file...
Code:
...Load CSS File into [b]CSSText[/b]...
...Transform XML with XSL into [b]XSLText[/b]...
[b]XSLText[/b] = Replace([b]XSLText[/b], _
                  "<LINK href='MyCSS.css' rel='stylesheet' />", _
                  "<style type='text/css'>" & [b]CSSText[/b] & "</style>")
...Save [b]XSLText[/b] as HTML file...
If I wanted to continue using the CSS for internal purposes

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top