我在一个简单(但有效)的替代方案上价值 2 便士(仅提供伪代码用于说明。谨慎操作:)
方法概要:
另一种解决方案可以包括一个简单的包装脚本(例如 shell、bash 脚本或其他)来调用您的主 xsl、使用名称 xslt 模式、主 xslt 文件、一个简单(空白)静态指定的 xslt 文件。
在主 xsl 中,包含一个静态 xsl 文件,它将调用/加载所有动态包含的 xslt。然后主 xsl 将在 2 种模式下运行:正常模式(未指定模式),它将加载包含在自身中的扩展 xsl 文件,以及在静态 xls 中,并处理任何输入文件,或者做它打算做的任何好事做。第二种模式,预处理器模式,将用于加载动态指定的 xsl 实例/文件。此模式将作为主处理运行的预处理器阶段调用。主 xslt 的处理流程是使用指定的预处理器模式调用它,然后使用指定的正常处理模式再次调用它。
实施提示:
为每个 xlator 定义一个 n 扩展名 xslt 文件 ext_xsl_container ,其目的是包含任何扩展名 xslt。
例如
<xsl:stylesheet >
<!-- main xslt -->
<xsl:import href="../xsl/ext_xsl_container.xsl/>
<!--param: list of dynamically specified extension xsl -->
<xsl:param name="extXslUrlList"/>
<!--param:preprocessor mode flag, with default set to false -->
<xsl:param name="preProcModeLoadXslF" select="false()" type="xs:boolean"
<!-- param: path to the staticall included ext_xsl_container: with default value set -->
<xsl:param name="extXslContainerUrl" select="'../xsl/ext_xsl_container.xsl'"/>
<xsl:if test=" ($preProcModeLoadXslF=true())" >
<xsl:call-template name="loadDynamicXsl" mode="preprocess_load_xsl"
</xsl:if>
....
</xsl:stylesheet>
ext_xslt_container 样式表将包含任何扩展 xslt。它可以在运行时通过编辑它(作为 xml 文档)动态更新,为扩展 xsl 样式表添加包含语句。
例如
<!-- ext xsl container : ext_xsl_container.xsl-->
<xsl:stylesheet
<xsl:include href="ext_xsl_container.xsl"/>
....
</xsl:stylesheet
创建一个小模板,比如 template_load_ext_xsl,指定模式,比如 mode="preprocess_load_xsl"
例如
<xsl:template name="loadDynamicXsl" mode="preprocess_load_xsl">
<!-- param: path to the staticall included ext_xsl_container-->
<xsl:param name="extXslContainerUrl"/>
<!--param: list of dynamically specified extension xsl -->
<xsl:param name="extXslUrlList"/>
<!-- step 1, [optional ] open the ext Xsl container file -->
<!-- step 2 [optional] clear contexts of the ext X -- >
<!-- step3 compile a list of include elements, one per each ext Xsl file -->
<!-- step 4 [optional] create a union of the include elements created with the content of the xsl container file : ie append content >
<!-- step 5 : write the union list of incudes to the ext XSL container file -->
<!-- DONE --->
</xsl:template>
模板将接受 ex_xsl_container 的名称和扩展 xsl 文件列表(包括它们的路径)作为参数
然后它将作为 xml 文档打开 ext_xsl_container 文件,为每个扩展添加(附加选项或清除文件并添加新代码)语句:xsl,保存文件并退出
接下来,当您以正常执行模式运行主 xsl 时,它将包含模板 loadDynamicXsl,该模板又将包含在运行时指定的扩展 xslt 文件
创建一个简单的包装脚本(例如 bash 或 shell 脚本),它将接受主 xslt 的参数,以及运行预处理器模式的选项。如果启用了预处理器模式选项,脚本将简单地调用主 xslt 两次,并在第一次运行时启用预处理器模式,然后在正常模式下进行第二次调用