合并多个 xslt 样式表 [英] Merge multiple xslt stylesheets

查看:28
本文介绍了合并多个 xslt 样式表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个 xsl:import 的 xslt 样式表,我想将它们全部合并到一个 xslt 文件中.

I have a xslt stylesheet with multiple xsl:imports and I want to merge them all into the one xslt file.

这是我们使用的系统的一个限制,它作为存储在内存中的字符串对象传递 xsl 样式表.这被传输到远程机器,在那里它执行转换.由于它不是从磁盘加载,因此 href 链接已损坏,因此我们需要从样式表中删除 xsl:import .

It is a limitation of the system we are using where it passes around the xsl stylesheet as a string object stored in memory. This is transmitted to remote machine where it performs the transformation. Since it is not being loaded from disk the href links are broken, so we need to remove the xsl:imports from the stylesheet.

是否有任何工具可以做到这一点?

Are there any tools out there which can do this?

推荐答案

您可以使用 XSL 样式表来合并样式表.但是,这等效于使用 xsl:include 元素,而不是 xsl:import(正如 Azat Razetdinov 已经指出的那样).您可以在此处阅读区别.

You can use an XSL stylesheet to merge your stylesheets. However, this is equivalent to using the xsl:include element, not xsl:import (as Azat Razetdinov has already pointed out). You can read up on the difference here.

因此,您应该首先将 xsl:import 替换为 xsl:include,解决所有冲突并测试您是否仍能获得正确的结果.之后,您可以使用以下样式表将现有样式表合并为一个.只需将其应用到您的主样式表:

Therefore you should first replace the xsl:import's with xsl:include's, resolve any conflicts and test whether you still get the correct results. After that, you could use the following stylesheet to merge your existing stylesheets into one. Just apply it to your master stylesheet:

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0">

<xsl:template match="xsl:include">
  <xsl:copy-of select="document(@href)/xsl:stylesheet/*"/>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

第一个模板通过使用 document 函数将所有 xsl:include 替换为包含的样式表,该函数读取 href 属性中引用的文件.第二个模板是 身份转换.

The first template replaces all xsl:include's with the included stylesheets by using the document function, which reads in the file referenced in the href attribute. The second template is the identity transformation.

我已经用 Xalan 对其进行了测试,它似乎运行良好.

I've tested it with Xalan and it seems to work fine.

这篇关于合并多个 xslt 样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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