如何在 xsl:apply-template 中测试匹配?

ste*_*eve 5 xslt

在我的第二个 xsl:template 匹配中,如何测试匹配模式?例如,如果匹配模式是标题,我想输出不同的值?

  <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>
Run Code Online (Sandbox Code Playgroud)

Lar*_*rsH 3

好问题,+1。

在第二个模板中,使用以下测试表达式:

test="self::title"
Run Code Online (Sandbox Code Playgroud)

或者

test="local-name() = 'title'"
Run Code Online (Sandbox Code Playgroud)

例如,您可以使用

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