<p>I have a xslt stylesheet with multiple <code>xsl:import</code>s and I want to merge them all into the one xslt file.</p>

<p>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 <code>xsl:import</code>s from the stylesheet.</p>

<p>Are there any tools out there which can do this?</p>

[{'answer_id': 70180, 'author': 'chrisb', 'author_id': 8262, 'author_profile': 'https://Stackoverflow.com/users/8262', 'pm_score': 0, 'selected': False, 'text': "<p>Why would you want to?  They're usually seperated for a reason afterall (often maintainability)</p>\n\n<p>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...</p>\n"}, {'answer_id': 70279, 'author': 'Robert Christie', 'author_id': 11069, 'author_profile': 'https://Stackoverflow.com/users/11069', 'pm_score': 1, 'selected': False, 'text': '<p>A Manual merge is probably going to be the best option. </p>\n\n<p>The main consideration will probably be to make sure that the logic for matching templates works in the combined stylesheet.</p>\n'}, {'answer_id': 70357, 'author': 'Azat Razetdinov', 'author_id': 9649, 'author_profile': 'https://Stackoverflow.com/users/9649', 'pm_score': 2, 'selected': False, 'text': '<p>It is impossible to include imported stylsheets into the main file without breaking <a href="http://www.w3.org/TR/xslt#dt-import-precedence" rel="nofollow noreferrer">import precedence</a>. 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.</p>\n\n<p>The workaround is two replace xsl:import’s with xsl:include’s and resolve any <a href="http://www.w3.org/TR/xslt#conflict" rel="nofollow noreferrer">conflicts</a>. After that you are safe to replace xsl:include instructions with the corresponding files’ contents, because that is what <a href="http://www.w3.org/TR/xslt#include" rel="nofollow noreferrer">XSLT-processor does</a>:</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n'}, {'answer_id': 70591, 'author': 'Christian Berg', 'author_id': 5035, 'author_profile': 'https://Stackoverflow.com/users/5035', 'pm_score': 5, 'selected': True, 'text': '<p>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 <a href="http://www.w3.org/TR/xslt#section-Combining-Stylesheets" rel="nofollow noreferrer">here</a>.</p>\n\n<p>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:</p>\n\n<pre><code>&lt;?xml version="1.0" ?&gt;\n&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" \n                version="1.0"&gt;\n\n&lt;xsl:template match="xsl:include"&gt;\n  &lt;xsl:copy-of select="document(@href)/xsl:stylesheet/*"/&gt;\n&lt;/xsl:template&gt;\n\n&lt;xsl:template match="@*|node()"&gt;\n  &lt;xsl:copy&gt;\n    &lt;xsl:apply-templates select="@*|node()"/&gt;\n  &lt;/xsl:copy&gt;\n&lt;/xsl:template&gt;\n\n&lt;/xsl:stylesheet&gt;\n</code></pre>\n\n<p>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 <a href="https://stackoverflow.com/questions/56837/how-can-i-make-an-exact-copy-of-a-xml-nodes-children-with-xslt">identity transformation</a>.</p>\n\n<p>I\'ve tested it with Xalan and it seems to work fine.</p>\n'}, {'answer_id': 1031921, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<pre><code> import multiple xsl in single xsl\n\n      &lt;xsl:import href="FpML_FXOption_Trade_Template1.xsl"/&gt;\n      &lt;xsl:apply-imports/&gt;\n\n&lt;calypso:keyword&gt;\n                &lt;calypso:name&gt;DisplayOptionStyle&lt;/calypso:name&gt;\n&lt;calypso:value&gt;Vanilla&lt;/calypso:value&gt;\n&lt;/calypso:keyword&gt;\n\n           &lt;xsl:import href="FpML_FXOption_Trade_Template2.xsl"/&gt;\n           &lt;xsl:apply-imports/&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/70143', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/716/']