开发者

how to test match in an xsl:apply-template?

In my second xsl:template match, how do I test for the match pattern? For example if the match patter is title, I want to output different value?

  <xsl:template match="secondary-content">
    <div class="secondary">
      <xsl:apply-templates select="title" />
      <xsl:apply-templates select="block/content | content" />
    </div>
  </xsl:template>
  <xsl开发者_开发知识库:template match="title|content|block/content">
    <xsl:copy-of select="node()" />
  </xsl:template>


Good question, +1.

In the second template, use this test expression:

test="self::title"

or

test="local-name() = 'title'"

For example, you can use

<xsl:choose>
  <xsl:when test="self::title">
    <someThing>foo</someThing>
  </xsl:when>
  <xsl:otherwise>
    <xsl:copy-of select="node()" />
  </xsl:otherwise>
</xsl:choose>


Why not split it into two separate template rules? It seems strange to have a single template rule to handle several cases when the logic is different for the different cases. Use separate rules, and if the logic is complex, factor common/shared logic into a named template (or if you're feeling ambitious, use xsl:next-match or xsl:apply-imports for the common logic).


It is almost always better not to have conditional logic within a template body.

Therefore, instead of:

<xsl:template match="title|content|block/content">
  <xsl:choose>
    <!-- conditional processing here -->
  </xsl:choose>
</xsl:template>

write:

<xsl:template match="title">
    <!-- Some processing here -->
</xsl:template>

<xsl:template match="content|block/content">
    <!-- Some other processing here -->
</xsl:template>

BTW, matching content|block/content is equivalent to the shorter content.

Therefore, the last template can be further simplified to:

<xsl:template match="content">
    <!-- Some other processing here -->
</xsl:template>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜