XSLT 条件包含外部文件 [英] XSLT conditional include of external file

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

问题描述

我想在 XSLT 中执行条件包含,但 xsl:include 是顶级元素.您只能在模板中使用 xsl:if 或 xsl:choose .是否有任何类型的黑客或解决方法允许有条件地包含外部文件?我尝试使用 document() 函数,但它无法加载我的外部文件(可能是因为它不符合某些使其有效"的规则集).

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 hack or work around which allows a conditional include of an external file? I tried to use the document() function, but it fails to load my external file ( probably because it doesn't conform to some set of rules which would make it "valid" ).

我的外部xml文件是一堆xslt代码片段.根据主 XSLT 文件中变量的值,来自外部文件的相应代码应该复制/粘贴"到位(如 C 或 PHP 中的条件包含).

My external xml file is a bunch of xslt code fragments. depending on the value of a variable in the main XSLT file, the corisponding code from the external file should be "copy/pasted" in place ( like conditional include in C or PHP ).

我的主要 XSLT 文件的流程应按以下方式进行:

The flow of my main XSLT file should proceed in the following manner:

$configurationMode
if ( $configurationMode = Standard ) { xsl:include="standard.xml" } else { xsl:include="alt.xml" }

显然我不能像上面那样简单地做到这一点,因此我为什么要问是否有破解或解决方法.

Obviously I cannot do it as simply as the above, hence why I am asking if there is a hack or workaround.

推荐答案

这在 XSLT 1.0 中无法完成,但可以在 XSLT 2.0 中使用 use-when 属性.

存在非 xslt 方法来实现所需的 xsl:includexsl:import 指令的动态更改.

There exist non-xslt ways of achieving the wanted dynamic altering of an xsl:include or an xsl:import directive.

其中一种方法是将 XSLT 样式表作为 XmlDocument 加载,并使用可用的 DOM 方法访问和修改属性,将 href 属性设置为所需的值.然后从这个在内存中修改的 XMLDocument 包含的 XSLT 样式表开始转换.

One such method is to load the XSLT stylesheet as an XmlDocument, and using the available DOM methods for access to and modification of attributes, to set the href attribute to the desired value. Then initiate the transformation from this in-memory-modified XMLDocument-contained XSLT stylesheet.

这篇关于XSLT 条件包含外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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