XSLT conditional include of external file

后端 未结 8 2042
情书的邮戳
情书的邮戳 2020-12-20 20:14

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

相关标签:
8条回答
  • 2020-12-20 20:46

    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.

    0 讨论(0)
  • 2020-12-20 20:46
    <xsl:variable name="CONDITIONAL" select="ELEMENT/CONDITION"/>
    <xsl:variable name="DOCUMENTNAME" select="document(concat($CONDITIONAL,'-DOCUMENT.xml'))/ELEMENT"/>
    

    This example is how I did a conditional use of an external file. I had the flag in the base XML that the XSL uses as a variable to help specify the name of the other file to pull into the template. Once that variable is defined as in my case I use it to pull other XML data into the generated output. The concat command forces the data together to give a file name.

    So When I want info from the external XML file I use '$DOCUMENTNAME' for referencing the external data.

    0 讨论(0)
提交回复
热议问题