【问题标题】:syntax of apply-template in xml stylesheetxml 样式表中应用模板的语法
【发布时间】:2012-06-02 12:51:55
【问题描述】:

以下代码中有一个名为“this-article”的变量。

一般情况下,我们这样使用“xsl:apply-templates”

<xsl:apply-templates select="somenode"/>

“somenode”表示子节点。


但是在这个变量中,apply-template是这样写的。很奇怪。

  <xsl:apply-templates select="." mode="id"/>

如果你能解释一下它的含义,我将不胜感激。

<!-- ============================================================= -->
<!--  "make-article" for the document architecture                 -->
<!-- ============================================================= -->

  <xsl:template name="make-article">
    <!-- Generates a series of (flattened) divs for contents of any
           article, sub-article or response -->

    <!-- variable to be used in div id's to keep them unique -->
    <xsl:variable name="this-article">
      <xsl:apply-templates select="." mode="id"/>
    </xsl:variable>

    <div id="{$this-article}-front" class="front">
      <xsl:apply-templates select="front | front-stub"/>
    </div>

    <!-- body -->
    <xsl:for-each select="body">
      <div id="{$this-article}-body" class="body">
        <xsl:apply-templates/>
      </div>
    </xsl:for-each>

    <xsl:if test="back | $loose-footnotes">
      <!-- $loose-footnotes is defined below as any footnotes outside
           front matter or fn-group -->
      <div id="{$this-article}-back" class="back">
        <xsl:call-template name="make-back"/>
      </div>
    </xsl:if>

    <xsl:for-each select="floats-group">
      <div id="{$this-article}-floats" class="back">
        <xsl:call-template name="main-title">
          <xsl:with-param name="contents">
            <span class="generated">Floating objects</span>
          </xsl:with-param>
        </xsl:call-template>
        <xsl:apply-templates/>
      </div>
    </xsl:for-each>

    <!-- more metadata goes in the footer -->
    <div id="{$this-article}-footer" class="footer">
      <xsl:call-template name="footer-metadata"/>
      <xsl:call-template name="footer-branding"/>
    </div>

    <!-- sub-article or response (recursively calls
             this template) -->
    <xsl:apply-templates select="sub-article | response"/>

  </xsl:template>

【问题讨论】:

    标签: xml stylesheet


    【解决方案1】:

    我不确切知道这段代码在做什么,但它是一种足够常见的设计模式。假设您想根据元素的“重要性”来决定是否显示(或省略)元素。那么你可能有一套计算元素重要性的规则:

    <xsl:template match="p" mode="importance">high</xsl:template>
    
    <xsl:template match="span[@class='x']" mode="importance">medium</xsl:template>
    
    <xsl:template match="emph[@class='x']" mode="importance">low</xsl:template>
    

    要计算当前元素的重要性,您可以这样做

    <xsl:variable name="importance">
      <xsl:apply-templates select="." mode="importance"/>
    </xsl:variable>
    

    这类代码通常由完全掌握模板规则在开发可扩展、基于规则、可重用和多态代码方面的价值的人编写。阅读这样的代码并从中学习非常值得。

    【讨论】:

      猜你喜欢
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多