Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl:import and xsl:include in stylesheets don't work on Google Chrome

I'm building a website that uses xsl stylesheets, and I'm building up a small library of useful functions 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 relevant stylesheet before its sent to the client?

like image 624
Moose Morals Avatar asked Oct 25 '09 10:10

Moose Morals


2 Answers

I'd do something like the following, which will combine the stylesheet serverside, before it gets to Chrome. The first step is in place because xsl:import is not the same as replacing all places with the imported stylesheets.

  1. Replace all xsl:import with xsl:include (import priority isn't applicable to xsl:include, so you may need to change your code and use priorities instead)
  2. Use the server-side stylesheet below to merge them into one before serving
  3. Wait a few weeks (can be months). I've created a fix for Chrome and am currently working with the developers team to include the fix into the build.
<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.

like image 61
Abel Avatar answered Nov 15 '22 06:11

Abel


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.

like image 26
Gabriel Hurley Avatar answered Nov 15 '22 07:11

Gabriel Hurley