Management includes in XSL

I have XSL generating code that includes several XSL files. Files are included for calling templates. There may be times when one of the files that I need to include does not exist in my file. I could create a dummy file with the same name and an empty template when the source file is missing, but I get a duplicate error if the source file exists.

The following is an example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:date="java.util.Date"                
    xmlns:vector="java.util.Vector"                
    xmlns:math="java.lang.Math"
    xmlns:int="java.lang.Integer"
xmlns:saxon="http://saxon.sf.net/"
    extension-element-prefixes="date vector math int saxon">
  <xsl:include href="file_for_template1.xsl"/> <!-- MIGHT NOT EXIST -->
  <xsl:include href="file_for_template2.xsl"/> <!-- MIGHT NOT EXIST -->
  <xsl:include href="file_for_template3.xsl"/> <!-- MIGHT NOT EXIST -->
  <xsl:output method="xml" 
              version="1.0" 
              encoding="UTF-8" 
              indent="yes"/>
  <xsl:variable name="path" 
                select="concat(base-uri(//Test),'.temp')"/>

  <xsl:template match="/"> 

    <xsl:result-document href="file:/{$path}" method="xml">

      <!-- create the root node -->
      <TEST_GENERATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                       xsi:noNamespaceSchemaLocation="test.xsd">

        <xsl:element name="TEST_ELEMENT">
          <!-- SHOULD NOT BE CALLED IF THE FILE DOES NOT EXIST -->
          <xsl:call-template name="template1Call"/>

          <!-- SHOULD NOT BE CALLED IF THE FILE DOES NOT EXIST -->
          <xsl:call-template name="template2Call"/>

          <!-- SHOULD NOT BE CALLED IF THE FILE DOES NOT EXIST -->
          <xsl:call-template name="template3Call"/>

        </xsl:element>

      <!-- end of root node -->
      </TEST_GENERATION>
      <!-- end of document -->
    </xsl:result-document>
  </xsl:template>
+4
source share
2 answers

, , , , , . , .

use-when , ( xsl: import/include declarations) . :

<xsl:import href="module1.xsl" 
            use-when="doc-available('module1.xsl')"/>

XSLT 2.0 , use-when , false, XSLT 3.0. 3.0 , , xsl: import , use-when.

, , , - . , function-available().

, , . , , , , , .

+3

, ( - , , , - ):

  • :

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:date="java.util.Date"                
        xmlns:vector="java.util.Vector"                
        xmlns:math="java.lang.Math"
        xmlns:int="java.lang.Integer"
    xmlns:saxon="http://saxon.sf.net/"
        extension-element-prefixes="date vector math int saxon">
      <xsl:include href="cfg:module:file_for_template1.xsl"/> <!-- MIGHT NOT EXIST -->
      <xsl:include href="cfg:module:file_for_template2.xsl"/> <!-- MIGHT NOT EXIST -->
      <xsl:include href="cfg:module:file_for_template3.xsl"/> <!-- MIGHT NOT EXIST -->
    
        ...
    </xsl:stylesheet>
    

- .

  1. . , "" , ( ).

    <?xml version="1.0" encoding="UTF-8"?>
    <catalog prefer="system" xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
        <uri name="cfg:module:file_for_template1.xsl" uri="path/to/default/file_for_template1.xsl"/>
        <uri name="cfg:module:file_for_template2.xsl" uri="path/to/default/file_for_template2.xsl"/>
        <uri name="cfg:module:file_for_template3.xsl" uri="path/to/default/file_for_template3.xsl"/>
    </catalog>
    

- XML , , XSL-T.

  1. , , , , , uri ( ) . , , XSL, .

  2. ( ). "" , "module" ( ). , . , "cfg:module:file_for_template1.xsl", , .

. XML.

+1

All Articles