管理包含在 XSL 中 [英] Manage includes in XSL

查看:24
本文介绍了管理包含在 XSL 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成 XSL 的代码,其中包含多个 XSL 文件.包含这些文件是为了调用模板.可能会出现我需要包含的文件之一在我的文件中不存在的情况.当原始文件丢失时,我可以创建一个同名的虚拟文件和一个空模板,但是当原始文件存在时,我会得到一个重复的错误.

I have a code generating XSL that includes several XSL files. The files are included in order to call templates. There can be the case 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 original file is missing but I would get a duplicate error when the original file exists.

下面是一个例子:

<?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>

推荐答案

我希望你能更多地告诉我们你试图解决的问题,而不是专注于你选择的方法遇到的困难.如果我们知道真正的需求,我们或许能够提出更好的解决方法.

I wish you had told us more about the problem you are trying to solve, rather than focusing on the difficulties you are having with your chosen approach. If we knew the real requirement, we might be able to suggest better ways of tackling it.

use-when 属性允许您进行这种编译时控制,使您能够根据外部条件有选择地排除样式表的一部分(包括 xsl:import/include 声明).你可以试试:

The use-when attribute allows you compile-time control of this kind, enabling you to selectively exclude parts of a stylesheet (including xsl:import/include declarations) based on external conditions. You could try:

<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 属性中引用该静态参数.

XSLT 2.0 says that in the context for evaluating use-when expressions there are no available documents, so this will always return false, but this rule changes in XSLT 3.0. In 3.0 you could also pass a static parameter to say whether an xsl:import should be active or not, and reference the static parameter in the use-when attribute.

您的下一个问题将是如果导入已被排除,调用模板指令将失败,除非您也采取措施取消激活这些指令.一种解决方案是用函数调用替换它们,您可以通过对 function-available() 的测试有条件地排除它们.

Your next problem will be that the call-template instructions will fail if the import has been excluded, unless you do something to de-activate these instructions as well. One solution would be to replace them with function calls, which you can conditionally exclude with a test on function-available().

但是我对在这条路上帮助你感到不安,这就是我之前没有回答这个问题的原因.我想我可能会把你带入沼泽地,如果我们知道你的真实目的地,我们可能会建议一条比你正在走的路线更好的路线.

But I'm uneasy about helping you down this road, which is why I didn't answer the question before. I think I might be leading you into a swamp, and that if we knew your real destination, we might be able to suggest a better route to it than the one you are taking.

这篇关于管理包含在 XSL 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆