XSLT中的主样式表共享

XSLT中的主样式表共享,xslt,Xslt,我想用XSLT创建一个主模板,它可以存储在一个单独的文件中。每隔一页样式表都会与xsl:import共享它 master.xslt page.xslt <xsl:stylesheet> <xsl:import href="master.xslt"/> <xsl:template match="/"> <apply-templates match="Page"/> </xsl:template> <xsl:templa

我想用XSLT创建一个主模板,它可以存储在一个单独的文件中。每隔一页样式表都会与xsl:import共享它

master.xslt


page.xslt

<xsl:stylesheet>
<xsl:import href="master.xslt"/>

<xsl:template match="/">
  <apply-templates match="Page"/>
</xsl:template>

<xsl:template name="Content">
  ... apply something page-specific
</xsl:template>

</xsl:stylesheet>
<xsl:stylesheet>
<xsl:import href="master.xslt"/>

<xsl:template match="/">
  <call-template name=masterHead>
   ... apply something page-specific
  <call-template name=masterEnd/>
</xsl:template>

</xsl:stylesheet>

... 应用特定于页面的内容
page.xml

<Page>
 ... something page-specific
</Page>

... 特定于页面的内容
我可以改进这个解决方案吗

  • 我不能从主样式表开始,因为我需要xsl:import所有内容
  • 我不希望master.xslt在每个特定页面上都包含引用
另一个决定(违背xslt精神)可能是这样的:

master.xslt


page.xslt

<xsl:stylesheet>
<xsl:import href="master.xslt"/>

<xsl:template match="/">
  <apply-templates match="Page"/>
</xsl:template>

<xsl:template name="Content">
  ... apply something page-specific
</xsl:template>

</xsl:stylesheet>
<xsl:stylesheet>
<xsl:import href="master.xslt"/>

<xsl:template match="/">
  <call-template name=masterHead>
   ... apply something page-specific
  <call-template name=masterEnd/>
</xsl:template>

</xsl:stylesheet>

... 应用特定于页面的内容

我们不需要任何通用的根元素。

对我来说很合适。。。这与我过去使用的非常常见(虽然我经常使用
,但两者都可以使用)。我可能做的主要更改是使匹配更加明确(至少在主xslt中是这样)


因此它不会意外地匹配其他位置的
页面
元素(例如,数据分页,如

我做的另一件常见的事情是添加一个“*”匹配,如果没有更具体的节点匹配,它使用
xsl:message
抛出错误。当你有打字错误等问题时,这会使问题变得更加明显。

使用是正确的设计决策。这正是XSLT指令的主要用途


我们可以更进一步——查找指令,以及导入的样式表如何应用模板,了解其操作和含义,但它完全不知道。后者被调用并用XSLT实现(完全用XSLT编写)。

我很高兴找到了这个示例,因为我一直在寻找验证,证明这实际上是主/从模板设置的正确方法。 然而,所提供的示例在tomcat上并不是开箱即用的——因此,为了帮助其他只知道如何复制粘贴的人,这里有一组工作的tomcat主/从文件

Master.xsl:

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" encoding="iso-8859-15" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" indent="no"/>
<!-- http://stackoverflow.com/questions/646878/master-stylesheet-sharing-in-xslt -->
 <xsl:template match="ms247">
  <html>
   <head>
    <title>test</title>
   </head>

   <body>
    <div style="border: 1px solid black; width: 200px; float: left; margin: 10px; padding: 5px;">
     <xsl:call-template name="left"/>
    </div>
    <div style="border: 1px solid black; width: 200px; float: left; margin: 10px; padding: 5px;">
     <xsl:call-template name="content"/>
    </div>
    <div style="border: 1px solid black; width: 200px; float: left; margin: 10px; padding: 5px;">
     <xsl:call-template name="right"/>
    </div>
   </body>
  </html>
 </xsl:template>


 <xsl:template name="content">
  <span style="color: red">Content template is empty - overrule in page template.</span>
 </xsl:template>

 <xsl:template name="left">
  <span style="color: red">Left template is empty - overrule in page template.</span>
 </xsl:template>

 <xsl:template name="right">
  <span style="color: red">Right template is empty - overrule in page template.</span>
 </xsl:template>
</xsl:stylesheet>

测试
内容模板为空-在页面模板中否决。
左侧模板为空-在页面模板中否决。
右侧模板为空-在页面模板中否决。
和slave.xsl:

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:import href="master.xsl"/>

 <xsl:template name="content">
  ... apply something page-specific
 </xsl:template>

 <xsl:template name="right">
  And we have RIGHT content!
  <!-- Execute matching template which is NOT triggered automatically -->
  <xsl:apply-templates select="params/param"/>
 </xsl:template>

 <!-- And we do not define any left template -->

 <!-- Example -->
 <xsl:template match="ms247/params/param">
  Paramters on page: <xsl:value-of select="@name"/><br/>
 </xsl:template>
</xsl:stylesheet>

... 应用特定于页面的内容
我们有正确的内容!
第页上的参数:

希望这能帮助其他人-不要羞于给我留言。

Aa领先/实际上不是一个好决定。@match属性的值是一个“匹配模式”,因此它不需要是一个绝对的XPath表达式。模板应用到哪些节点取决于选择它的节点的(动态)上下文。@Dimitre:如果只希望它与根页面元素匹配,这是个好主意。
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:import href="master.xsl"/>

 <xsl:template name="content">
  ... apply something page-specific
 </xsl:template>

 <xsl:template name="right">
  And we have RIGHT content!
  <!-- Execute matching template which is NOT triggered automatically -->
  <xsl:apply-templates select="params/param"/>
 </xsl:template>

 <!-- And we do not define any left template -->

 <!-- Example -->
 <xsl:template match="ms247/params/param">
  Paramters on page: <xsl:value-of select="@name"/><br/>
 </xsl:template>
</xsl:stylesheet>