我有以下模板
<h2>one</h2>
<xsl:apply-templates select="one"/>
<h2>two</h2>
<xsl:apply-templates select="two"/>
<h2>three</h2>
<xsl:apply-templates select="three"/>
我只想在相应模板中至少有一个成员时显示标题(一,二,三)。我该如何检查?
最佳答案
<xsl:if test="one">
<h2>one</h2>
<xsl:apply-templates select="one"/>
</xsl:if>
<!-- etc -->
或者,您可以创建一个命名模板,
<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>
然后调用为:
<xsl:call-template name="WriteWithHeader">
<xsl:with-param name="header" select="'one'"/>
<xsl:with-param name="data" select="one"/>
</xsl:call-template>
但老实说,这对我来说似乎需要更多工作...仅在绘制 header 很复杂的情况下才有用...对于一个简单的
<h2>...</h2>
,我很想将其保留为内联。如果 header 标题始终是节点名,则可以通过删除“$ header” arg来简化模板,而改用:
<xsl:value-of select="name($header[1])"/>
关于xslt - 如何检查XSLT中是否存在标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/195240/