Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple xslt stylesheets

Tags:

xslt

I have a xslt stylesheet with multiple xsl:imports and I want to merge them all into the one xslt file.

It is a limitation of the system we are using where it passes around the xsl stylesheet as a string object stored in memory. This is transmitted to remote machine where it performs the transformation. Since it is not being loaded from disk the href links are broken, so we need to remove the xsl:imports from the stylesheet.

Are there any tools out there which can do this?

like image 697
roo Avatar asked Sep 16 '08 07:09

roo


1 Answers

You can use an XSL stylesheet to merge your stylesheets. However, this is equivalent to using the xsl:include element, not xsl:import (as Azat Razetdinov has already pointed out). You can read up on the difference here.

Therefore you should first replace the xsl:import's with xsl:include's, resolve any conflicts and test whether you still get the correct results. After that, you could use the following stylesheet to merge your existing stylesheets into one. Just apply it to your master stylesheet:

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

<xsl:template match="xsl:include">
  <xsl:copy-of select="document(@href)/xsl:stylesheet/*"/>
</xsl:template>

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

</xsl:stylesheet>

The first template replaces all xsl:include's with the included stylesheets by using the document function, which reads in the file referenced in the href attribute. The second template is the identity transformation.

I've tested it with Xalan and it seems to work fine.

like image 158
Christian Berg Avatar answered Jan 02 '23 03:01

Christian Berg