tags:

views:

1682

answers:

5

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?

A: 

Why would you want to? They're usually seperated for a reason afterall (often maintainability)

You could always write the merge yourself - read the XSL files in, select the template items you're interested in and write to a new master XSL file...

chrisb
user explains: "(the file is) transmitted to remote machine where it performs the transformation. Since it is not being loaded from disk the href links are broken"
Steve Cooper
i edited the quetion to include the reason. since this answer was posted before that edit.
roo
+1  A: 

A Manual merge is probably going to be the best option.

The main consideration will probably be to make sure that the logic for matching templates works in the combined stylesheet.

Robert Christie
That is what I was afraid of :\
roo
+2  A: 

It is impossible to include imported stylsheets into the main file without breaking import precedence. For example, you define a top-level variable in an imported stylesheet and redefine it in the main file. If you merge two files into one, you’ll get two variables with the same name and import precedence, which will result in an error.

The workaround is two replace xsl:import’s with xsl:include’s and resolve any conflicts. After that you are safe to replace xsl:include instructions with the corresponding files’ contents, because that is what XSLT-processor does:

The inclusion works at the XML tree level. The resource located by the href attribute value is parsed as an XML document, and the children of the xsl:stylesheet element in this document replace the xsl:include element in the including document. The fact that template rules or definitions are included does not affect the way they are processed.

Azat Razetdinov
+6  A: 

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.

Christian Berg
A: 
 import multiple xsl in single xsl

      <xsl:import href="FpML_FXOption_Trade_Template1.xsl"/>
      <xsl:apply-imports/>

<calypso:keyword>
    <calypso:name>DisplayOptionStyle</calypso:name>
<calypso:value>Vanilla</calypso:value>
</calypso:keyword>

           <xsl:import href="FpML_FXOption_Trade_Template2.xsl"/>
           <xsl:apply-imports/>