XSLT conditional include of external file

后端 未结 8 2072
情书的邮戳
情书的邮戳 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条回答
  •  -上瘾入骨i
    2020-12-20 20:26

    With the addition of static parameters, this is now possible in XSLT 3.0. Static parameters can be used in the use-when attribute of the xsl:include.

    Now we can declare parameters with default values of false() and then override the ones we need at run time...

      
    
    

    Here is a full working example tested with Saxon-HE v9.7 (also tested with Saxon-PE 9.5).

    XML Input (test.xml)

    
        
    
    

    Main XSLT 3.0 (test_main.xsl)

    
      
      
    
      
      
    
      
      
    
      
        
          
        
      
    
    
    

    First possible included XSLT 3.0 (test_inc1.xsl)

    
    
      
        INCLUDE FILE 1!!!
      
    
    
    

    Second possible included XSLT 3.0 (test_inc2.xsl)

    
    
      
        INCLUDE FILE 2!!!
      
    
    
    

    Command line (setting inc2 to true)

    java -cp "saxon9he.jar" net.sf.saxon.Transform -s:"test.xml" -xsl:"test_main.xsl" inc2="true"
    

    Output

    
       INCLUDE FILE 2!!!
    
    

提交回复
热议问题