tags:

views:

356

answers:

1

How can I add stylesheet reference with XSLT?

I'm trying to strip down some large input XML with the first transform, and need the second transform to be applied on the client. Thus the first transform has to output the correct reference, e.g.:

<?xml-stylesheet type="text/xsl" href="client.xsl"?>

To recap it's XML->transform1(server)->XML->transform2(client)->HTML

The only way I can make it to work so far is by using xsl:text disable-output-escaping and CDATA:

<xsl:text disable-output-escaping="yes"><![CDATA[<?xml-stylesheet type="text/xsl" href="/efo/efo_class.xsl"?>]]>

Surely there must be a better method.

+4  A: 

From the XSLT spec, Creating Processing Instructions:

<xsl:processing-instruction name="xml-stylesheet">
  <xsl:text>href="book.css" type="text/css"</xsl:text>
</xsl:processing-instruction>

would create the processing instruction:

<?xml-stylesheet href="book.css" type="text/css"?>

(Example beautified by Tomalak's suggestion)

legoscia
+1 - I would probably wrap the value in an `<xsl:text>` and add line breaks to increase readability.
Tomalak
Good idea, edited. You should be writing the examples in the spec :)
legoscia
Given the fact that most code samples in white papers, specs and knowledge bases and so on serve as blueprint "good code" examples, they tend to get much too little love.
Tomalak
It has to be type="text/xsl" but other than that it solved the problem. Thanks!
Taveren
@Taveren: The code way copied off the spec verbatim. A *little* cognitive work must be left to the one who asked the question, don't you think? ;-)
Tomalak
@Tomalak Surely, but I would hate to come across this question only to get a partial answer :P
Taveren
This depends on how you define "partial". The question was "how to create a processing instruction in XSLT, like this one". And in this regard the answer is complete and correct.`</nitpick>` ;-)
Tomalak