XSLT conditional include of external file

xsl:iterate
xslt transformation examples
xslt example
xslt functions
xslt tutorial
xslt for-each
xsl:copy-of
xslt json output

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 hack or work around which allows a conditional include of an external file? I tried to use the document() function, but it fails to load my external file ( probably because it doesn't conform to some set of rules which would make it "valid" ).

My external xml file is a bunch of xslt code fragments. depending on the value of a variable in the main XSLT file, the corisponding code from the external file should be "copy/pasted" in place ( like conditional include in C or PHP ).

The flow of my main XSLT file should proceed in the following manner:

$configurationMode
if ( $configurationMode = Standard ) { xsl:include="standard.xml" } else { xsl:include="alt.xml" }

Obviously I cannot do it as simply as the above, hence why I am asking if there is a hack or workaround.

This cannot be done with XSLT 1.0 and can be done (to a very limited extent) in XSLT 2.0 using the use-when attribute.

There exist non-xslt ways of achieving the wanted dynamic altering of an xsl:include or an xsl:import directive.

One such method is to load the XSLT stylesheet as an XmlDocument, and using the available DOM methods for access to and modification of attributes, to set the href attribute to the desired value. Then initiate the transformation from this in-memory-modified XMLDocument-contained XSLT stylesheet.

XSLT conditional include of external file - xml - html, You can only use xsl:if or xsl:choose inside of a template. Is there any kind of hack or work around which allows a conditional include of an external file? I tried to  Using the xsl:import instruction is just as easy as with xsl:include, though it has to go before any other childnode of the main stylesheet. File: RenderTimeStampedImages.xslt As you can see, using the xsl:import instruction lets you override the imported templates, but at the same time, has the ability to execute the original, preserving its context, via the xsl:apply-imports instruction.

As I understand things, 'include' happens when the xml parser is parsing and compiling the style sheet. This means that no logic or expression evaluation can happen before the include gets processed and therefore there's no way to make it conditional as such. You need to make the conditional behavior happen outside of the style sheet.

Have a look at this http://www.dpawson.co.uk/xsl/sect2/N4760.html#d6065e100

In particular does this comment by Mike Kay help:

This has been raised a number of times. On a previous thread we came to the conclusion that the user was trying to write a general-purpose stylesheet G and then specialize it by conditionally including a special-purpose stylesheet A or B. The way to meet this requirement is to have A and B include G, not the other way around, and then you conditionally select A or B as the principal stylesheet when starting the transformation.

XSL Transformations (XSLT) Version 3.0, 8.1 Conditional Processing with xsl:if; 8.2 Conditional Processing with xsl:choose​; 8.3 Try/Catch A stylesheet generally includes elements that are defined by XSLT as To make streamed processing of unparsed text files easier, the For all functions that access external data, including document , doc  If the menu XML file is in the 'menu.XML' file (in the same directory as the XSLT stylesheet file, then this transformation produces exactly the same result as the previous: Typically, one defines a global-level variable, whose value is the result of calling the document() function.

Try inverting the structure: if you have two special purpose modules pink.xsl and blue.xsl, and a general-purpose module baby.xsl, then instead of trying to import/include one of pink.xsl or blue.xsl into baby.xsl, instead use pink.xsl or blue.xsl as the top-level entry stylesheet, and have each of these two import baby.xsl. That's the way it was designed to be used, it's nto a hack or a workaround.

Alternatively, given this description of your scenario "My external xml file is a bunch of xslt code fragments", a better approach in your case might be to assemble the stylesheet from these fragments as a separate step, using an XSLT tranformation rather than using xsl:include/xsl:import.

XSL Transformations (XSLT) Version 1.0, In addition to XSLT, XSL includes an XML vocabulary for specifying formatting. selecting elements for processing, for conditional processing and for generating text. If an element or processing instruction occurs in an external entity, the base URI of <xsl:comment>This file is automatically generated. Conditional AND OR two different values. xslt conditional conditional-statements. Split XML file by number of records/treshold and copy header - XSLT 1.0

Not sure if this would apply to your scenario, but I'm going to throw this out there anyways.

I've done similar things in the past, but instead of doing a conditional include, I call a template in either one of the two files based on what the xsl:when evaluates to. If you don't want to use templates in your case, disregard this answer.

For instance, say the "parent" xslt file is called root.xslt, and the two conditionally-used ones are child1.xslt and child2.xslt. Say I want to run conditional logic against the value of a node called status. If the value is "CURRENT", I want to call a template called isCurrent in child1.xslt, otherwise call a template called isNotCurrent in child2.xslt. For the sake of brevity, in each case I'm simply passing the template the root node and all children.

It'd look something like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:import href="child1.xslt"/>
    <xsl:import href="child2.xslt"/>

    <xsl:template match="/">
      <xsl:choose>
        <xsl:when test="rootNode/status = 'CURRENT'">
          <!-- template isCurrent defined in child1.xslt -->
          <xsl:apply-templates select="rootNode" mode="isCurrent" />
        </xsl:when>
        <xsl:otherwise>
          <!-- template isNotCurrent defined in child2.xslt -->
          <xsl:apply-templates select="rootNode" mode="isNotCurrent" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Hope this at least helps a little bit.

[PDF] XSLT Best practices, There are three use cases for including external files in your xsl: 1. You have additional If you have a xml file that you want to include in your output, you need to use the Oliver Becker's method of conditional selection. Xpath's ablility to  XSLT 2.0, XPath 2.0, and XQuery 1.0, share the same functions library. There are over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, and more.

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.

Analysis, Architectures and Modelling of Embedded Systems: Third , macros, integrating include files and evaluation of conditional compilation directives. Syntax Tree In the third phase, the generated XML file is processed with XSLT. parts that have been added by the integration of external header files. XSLT <xsl:if> Element. The <xsl:if> element is used to put a conditional test against the content of the XML file. The <xsl:if> Element. To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.

Creating Transformations with the XSLT Map Editor, How to Create an XSLT Map · How to Create an XSL Map File in Oracle BPEL Process Manager · How to Importing an External XSLT Map The XSLT View includes the same panes as the Map View, except that the right target pane is divided into two panes. How to Add Conditional Processing Using xsl:choose. If you need specific items out of your html file, you can even set a path, given that your html is well-formed. You can use unparsed-text() function which can read external file in. Or if you know that the external file is a valid xml, you can use document() function as well. Both XSLT 2.0 functions however - i think.

Creating Transformations with the XSLT Mapper, For information on invoking the XSLT Mapper from Oracle BPEL Process Manager, see Section 40.2.1, Add the template to an external file that you include in your XSL map. Description of "Figure 40-26 Conditional Processing with xsl:if". The <xsl:import> element is a top-level element that is used to import the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the importing style sheet. Note: This element must appear as the first child node of <xsl:stylesheet> or <xsl:transform>.

[PDF] XSLT Tutorial, multiple programs via <<xsl:include> and <xsl:import> XSLT elements. five data types available: boolean, number, string, node-set, external object. • addition with XSLT 1.1: the template looks like a standard HTML file. looks very like a conventional procedural program with variables, conditional statements, loops  The XML FAQ — Frequently-Asked Questions about the Extensible Markup Language. You can't as such: XML isn't a programming language, so you can't say things like. But you can have conditional criteria in a Schema, DTD, or a processor, and some DTDs provide attributes for conditional processing. If you need to make an element optional