2014-03-04 49 views
3

我正在维护一个xsl-fo文件。我既不擅长xslt,也不在xsl-fo,每年我只接触一次该文件。如何重新使用xsl-fo块?

我注意到该文件中有很多重复。例如,我在不同的上下文中有两次(逐字)确切区块:

   <xsl:if test="ReleaseNote!=''"> 
        <fo:block background-color="#ffeeee" padding="5mm" font-size="12pt" text-indent="0" border-style="solid" border-width="1px" border-color="red"> 
        <fo:table table-layout="fixed" width="100%"> 
         <fo:table-column column-width="20mm" /> 
         <fo:table-column column-width="135mm" /> 
         <fo:table-body> 
         <fo:table-row> 
          <fo:table-cell margin="0" padding="0" text-align="justify"> 
          <fo:block text-align="justify"> 

           <fo:external-graphic src="pic/warning.png" content-width="12mm" content-height="12mm" /> 
          </fo:block> 
          </fo:table-cell> 
          <fo:table-cell margin="0" padding="0"> 
          <fo:block linefeed-treatment="preserve"> 
           <xsl:value-of select="ReleaseNote" /> 
          </fo:block> 
          </fo:table-cell> 
         </fo:table-row> 
         </fo:table-body> 
        </fo:table> 
        </fo:block> 
       </xsl:if></fo:block> 

如何删除此重复?我可以预先定义这个块,然后在xslt中重新使用它的“引用”吗?

+0

可能重复[如何在XSL中重复使用代码](http://stackoverflow.com/questions/15711840/how-can-i-reuse-code-in-xsl) –

回答

2

你可以把这个放在的命名模板内。更多关于here规范相关部分中命名模板(特别是参数)的详细信息。

<xsl:template name="reusable-content"> 
    <xsl:if test="ReleaseNote!=''"> 
     <!--...--> 
    </xsl:if> 
</xsl:template> 

然后,调用你需要的内容的模板:

<xsl:call-template name="reusable-content"/> 

的命名模板的优点是调用模板的情况下被保留。换句话说,在原始代码中工作的任何XPath表达式在命名模板内执行时也可以完美地工作。 这是唯一重要的,如果代码取决于上下文,为你做:

<xsl:value-of select="ReleaseNote" /> 

指令上方依赖于上下文,其中ReleaseNote可以作为一个子元素。


你的问题的另一个解决办法是将存储在可变可重用内容。但是可能会有一些限制,特别是如果您使用XSLT 1.0(您没有透露您使用的是哪个版本)。