如何检查XSLT中是否存在标记?

Man*_*anu 9 tags xslt

我有以下模板

<h2>one</h2>
<xsl:apply-templates select="one"/>
<h2>two</h2>
<xsl:apply-templates select="two"/>
<h2>three</h2>
<xsl:apply-templates select="three"/>
Run Code Online (Sandbox Code Playgroud)

如果相应模板中至少有一个成员,我只想显示标题(一,二,三).我该如何检查?

Mar*_*ell 15

<xsl:if test="one">
  <h2>one</h2>
  <xsl:apply-templates select="one"/>
</xsl:if>
<!-- etc -->
Run Code Online (Sandbox Code Playgroud)

或者,您可以创建一个命名模板,

<xsl:template name="WriteWithHeader">
   <xsl:param name="header"/>
   <xsl:param name="data"/>
   <xsl:if test="$data">
      <h2><xsl:value-of select="$header"/></h2>
      <xsl:apply-templates select="$data"/>
   </xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

然后打电话给:

  <xsl:call-template name="WriteWithHeader">
    <xsl:with-param name="header" select="'one'"/>
    <xsl:with-param name="data" select="one"/>
  </xsl:call-template>
Run Code Online (Sandbox Code Playgroud)

但说实话,这看起来对我来说更有用......只有在绘制标题时很有用...对于一个简单的<h2>...</h2>我很想把它留在内联.

如果标题标题始终是节点名称,则可以通过删除"$ header"arg来简化模板,并使用:

<xsl:value-of select="name($header[1])"/>
Run Code Online (Sandbox Code Playgroud)