I want to perform a conditional include in XSLT, but xsl:include is a top level element. You can only use xsl:if or xsl:choose inside of a template. Is there any kind of hac
In addition to what already has been said, a possible solution would be to make the supplementary files to be plain, content-providing XML files (instead of XSLT files). That way, you can include them with XPath's document()
function (which will be evaluated at run-time rather than compile-time).
You can then alter the behavior of your transformation based on the contents of the loaded XML document; however, you cannot provide executable XSLT fragments in the included documents.
It depends on your use-case whether this is a solution - if the additional documents have a strong impact on the control flow of your transformation, you don't want to define them in plain XML, because you basically would re-implement something like XSLT. However, when your documents serve as kind of configuration files, you might want to think about providing them als pure XML.
When you're having problems with document()
, use an XML validator on your files. An error implies that your files are not valid XML.