1

次のコードには、「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>
4

1 に答える 1

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-06-02T17:51:18.047 に答える