15

how do i implement css in xsl file? i tried this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<link rel="stylesheet" type="text/css" href="dxsl.css"/>
<xsl:output method="html" />

but it threw the error:

XSLTProcessor::importStylesheet(): Found a top-level element link with null namespace URI 

and

Warning: XSLTProcessor::transformToXml(): No stylesheet associated to this object 
1
  • I think this is not an XSLT question but a HTML question. If you run a transformation from XML to HTML then ¿Where do you output the link element? Commented Jul 31, 2010 at 21:41

4 Answers 4

8

Your html (link tag) must be inside an xsl:template. The xsl:template must be inside an xsl:stylesheet.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" />

<xsl:template match="/*//*[1]">

    <link rel="stylesheet" type="text/css" href="dxsl.css"/>

</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

2 Comments

this doesn't make sense if you are applying xsl to transform xml to xml.
Then it's a good thing the question was about transforming xml to html.
4

If I understand you correctly, you wish for the output to have a particular stylesheet?

XSL is a language used to transform XML from one format to another (in a sense it's like applying a css stylesheet). What would happen in a typical use case is that you would take some xml file and use XSL to transform it, say to XHTML. In this output, you can include a stylesheet using the link element if you wanted, but XSL doesnt really make use of CSS as such. (So basically, try putting the CSS in the XSL as part of the transformation to have the XHTML output use it.)

If this is an XML document you simply need to include the reference to the XSL and it should handle the transformation for you automatically.

1 Comment

finally, I understand now what is going on ... good answer (if you have html elements in .xsl file, there is needful to add css href link under <html> tag as in normal HTML, because as output XHTML would have line to import css file as a result)
1

'link' is a HTML element and you're trying to use it as an XML one. XSL modifies input into another document. You don't use CSS in an XSL file. You insert it into an (X)HTML file and apply it there.

2 Comments

thanks, but i got it working. it has to be just after the head element in the xsl file. http://www.velocityreviews.com/forums/t166257-linking-to-a-separate-css-in-xsl.html
Yes, because, once inside the head, you are in the HTML namespace.
-1

As another answer said, the link must be generated by the transform. Here's an example that generates a small HTML doc with link to CSS from a simple xml doc. The css path comes from the data as well.

Try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.