动态决定使用哪个 XSL 样式表 [英] Dynamically decide which XSL stylesheet to use

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

问题描述

我正在尝试创建一个站点,该站点(除其他外)将显示包含在 xml 文件中的数据.我正在使用 xsl 样式表来格式化所有内容,但某些页面具有相似的内容.有没有一种方法可以告诉 xsl 数据的显示位置并让它确定要使用的布局,而不是使用重复数据制作多个 xml 表.

I'm trying to create a site which (among other things) will display data which is contained in xml files. I'm using xsl stylesheets to format everything, but some of the pages have similar content. Rather than have to make multiple xml sheets with duplicate data, is there a way to tell the xsl where the data is being displayed and have it determine which layout to use.

示例:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<xsl:choose>
  <xsl:if test="something">
    <!-- Format data one way -->
  </xsl:if>
  <xsl:otherwise>
    <!-- Format data another way -->
  </xsl:otherwise>
</xsl:choose>

</xsl:template>
</xsl:stylesheet>

该站点托管在一个更大的站点上,该站点不允许其微型站点使用任何服务器端脚本,因此我的选择在此处受到严重限制.

The site is being hosted on a larger site which doesn't allow its microsites to use any server side scripting so my options are severely limited here.

推荐答案

在这种情况下,我使用布局,每个布局都包含在一个单独的 XML 文档中.

要使用的(文件名)布局可以作为参数传递给转换,也可以在转换中动态确定.

The (filename of the) layout to use can be passed as a parameter to the transformation, or it can be dynamically determined within the transformation.

从现在开始,可以使用 XSLT document() 函数访问 Layout XML 文档:

From this moment on, the Layout XML document can be accessed using the XSLT document() function:

<xsl:variable name="vDocLayout" select="document($pLayout)"/>

然后你可以发出:

<xsl:apply-templates select="$vDocLayout"/>

这是填空"XSLT 设计模式.

这篇关于动态决定使用哪个 XSL 样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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