tags:

views:

318

answers:

4

Hi, I'm building a website that uses xsl stylesheets, and I'm building up a small library of useful funtions in a util stylesheet that other sheets import with

<xsl:import href="util" />

at the top of every sheet. This doesn't work in Google Chrome, as it doesn't support xsl:import yet. Can someone please write me a stylesheet that I can run on the server side that will read the xsl:import line and import the relevent stylesheet before its sent to the client? Thanks.

+1  A: 

Try something like this in php:

<?php
$sXml  = "<xml>";
$sXml .= "<testtag>hello tester</testtag>";
$sXml .= "</xml>";

# LOAD XML FILE
$XML = new DOMDocument();
$XML->loadXML( $sXml );

# START XSLT
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load( 'xsl/index.xsl', LIBXML_NOCDATA);
$xslt->importStylesheet( $XSL );
#PRINT
print $xslt->transformToXML( $XML );
?>


Paul McMillan
+2  A: 

You could do it in Python with the libxml2 and libxslt modules... not to do all your work for you, but starting with something like this:

import libxml2, libxslt

styledoc = libxml2.parseFile("page.xsl")
style = libxslt.parseStylesheetDoc(styledoc)
doc = libxml2.parseFile("somefile.xml")
result = style.applyStylesheet(doc, None)

Then just serve the thing back out.

Gabriel Hurley
A: 

http://www.w3.org/TR/xslt#literal-result-element shows how to solve the duplicate-xsl-namespace issue when writing an XSL stylesheet which transforms your existing XSL stylesheet into an XSL stylesheet with the <xsl:import>s expanded.

Be careful, though, about the difference between <xsl:import> and <xsl:include>.

ndim
+2  A: 

I'd do something like the following, to make it work and prevent having to deal with import priorities:

  1. Replace all xsl:import with xsl:include. Test and fix until they work again
  2. Use the server-side stylesheet below to merge them into one before serving
  3. Wait a few weeks (can be months). I've create the fix for Chrome and am currently working with the developers team to include the fix into the build.

(an SO bug: no text after list, no code?)

<xsl:template match="node()">
    <xsl:copy>
       <xsl:copy-of select="@*"/>
       <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="xsl:include">
   <!-- you'll probably want to be a bit more restrictive here -->
   <xsl:copy-of select="document(@href)/xsl:stylesheet/*" />
</xsl:template>

Update: Just a note: the Chrome bug appears in Safari too.

Abel
Thanks, just what I needed. Nice to hear about the long term fix too!
Moose Morals
You're welcome :) I'll report the SO bug to StackOverflow (if I remove that cursive text, the whole block of code disappears... odd).
Abel