XSLT样式表设计模式 - 在不同的样式表中分离常用和特定模板

时间:2010-02-17 17:15:45

标签: design-patterns xslt fxsl

我想知道在XSLT样式表设计模式中是否有一个很好的方法来分隔常见和特定的数据表示。

我在尝试,但却非常困惑和失落。我将不胜感激任何有关如何更好地分离XSLT样式表的建议,提示和提示。而且,非常感谢以下示例的帮助,因为它不起作用= /谢谢!

我需要创建各种具有不同外观的HTML文档,这些文档将重用某些数据。例如文档的日期,签名详细信息(名称,职称)等。此外,我使用了很多全局变量(因为XML结构不好,数据在整个文档中重复使用)。

我试图做的是移动所有可以在一个样式表中创建一个公共HTML结构的模板,然后所有特定的位都将在他们自己的样式表中。

如下所示:

常见的模板样式表“commonTemplates.xsl”

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Variables Local -->
...

<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" 
    doctype-system="http://www.w3.org/TR/html4/loose.dtd" indent="yes" />

<xsl:template match="/Bookings">
    <html>
        <head>
            <!-- populated by a template in a specific stylesheet -->
            <title><xsl:call-template name="docTitle"/></title>
        </head>
        <body>
            <xsl:apply-templates />
        </body>
    </html>
</xsl:template>

<!-- general template for date -->
<xsl:template match="/Bookings/Booking" name="docDate">
  <p class="date"><xsl:value-of select="./@Date"/></p>
</xsl:template>

<!-- general template for signature -->
<xsl:template match="/Bookings/Booking/Users" name="signature">
    <xsl:param name="signatureLine" select="'Yours sincerely,'"/>
    <div id="signature">
        <p><xsl:value-of select="$signatureLine"/></p>
        <p class="details">
            <!-- populated by a template in a specific stylesheet -->
            <xsl:apply-templates select="." mode="signature"/>
        </p>
    </div>
</xsl:template>

<!-- dummy templates signatures otherwise it complains that there is no such template -->
<xsl:template name="docTitle"/>    
</xsl:stylesheet>

特定的模板样式表:

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

<!-- Imports -->
<xsl:import href="commonTemplates.xsl"/>

<!-- BODY CONTENT OF HTML PAGE -->
<xsl:template match="/Bookings/Booking">
    <xsl:call-template name="docDate"/>
    <!-- document's content -->
    <div>
        <xsl:call-template name="content" />
    </div>
</xsl:template>


<xsl:template name="docTitle">
    <xsl:text>Here is the document title</xsl:text>
</xsl:template>

<!-- some content at the end of which signature should be inserted -->
<xsl:template name="content">
    <p>SOME CONTENT</p>        
    <xsl:apply-templates />
</xsl:template>

<!-- specific rule to insert appropriate data for signature -->
<xsl:template match="/Bookings/Booking/Users" mode="signature">
    <span class="name"><xsl:value-of select="./@Name"/></span>
    <span class="job"><xsl:value-of select="./@Title"/></span>
</xsl:template>
</xsl:stylesheet>

不幸的是,用于签名的模板不起作用,我无法弄清楚原因:(虽然它适用于docTitle。

结果HTML如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title></title>
   </head>
   <body>      
      <p class="date">16 February 2010</p>
      <div id="secondDeposit">
         <p>SOME CONTENT</p>
         <!-- here I get lots of empty space -->
      </div>    
   </body>

我想知道这个想法是否可以在一般情况下实施,以及如何正确实施,显然我的工作不起作用。

此外,在这种情况下哪种方法会更好:包含还是导入样式表?我想其中一个我不需要再次列出所有变量。

我将不胜感激任何帮助!很抱歉很长的帖子,如果不是很清楚。

谢谢!

2 个答案:

答案 0 :(得分:3)

我使用以下方法:

模板文件(/templates/Main.xslt)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="utf-8" />
  <xsl:include href="../functions/General.xslt"/>
  <xsl:template match="/">
    <html xml:lang="en">
      <head>
      </head>
      <body>
         Template Content
         <xsl:call-template name="PageContent" />
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

函数文件(/functions/General.xslt)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Generic functions used in the site go in here -->
</xsl:stylesheet>

页面文件(/default.xslt)

将会有一些在模板中定义不同的布局。这是您进行转换时调用的XSLT文件。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="templates/Main.xslt" />
    <xsl:output method="xml" encoding="utf-8" />
    <xsl:template match="/">
        <xsl:apply-imports />
    </xsl:template>
    <!-- CONTENT -->
    <xsl:template name="PageContent">
      <!-- Put your specific page stuff here -->
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:2)

建议最好的方法是在样式表文件中累​​积常用的模板,这些模板将由特定的XSLT应用程序导入。这些有用的xstylesheet文件的集合可以提供有用的

当然,导入的样式表模块通常会导入其他样式表模块。

一个例子是用于XSLT中函数编程的FXSL library。请查看XSLT应用程序(示例testxxx.xsl文件)如何导入所需的功能。