how to find out if an attribute exists or not in XSL.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Just use:
<xsl:template match="someElement/@someAttrName">
<!-- Whatever specific work when someElement has @someAttrName -->
</xsl:template>
<xsl:template match="someElement[not(@someAttrName)]">
<!-- Whatever specific work when someElement has no @someAttrName -->
</xsl:template>
Do note: In a well-written XSLT code the number of conditional instructions (such as <xsl:choose>
, <xsl:when>
, <xsl:otherwise>
, <xsl:if>
, ... etc.) is close to zero. In this solution it is 0.
回答2:
<xsl:choose>
<xsl:when test="element/@attribute">
do one thing
</xsl:when>
<xsl:otherwise>
do something else
</xsl:otherwise>
</xsl:choose>
回答3:
<xsl:value-of select="element[not(@attribute)]"/>
if need select some element without attribute