how to find out if an attribute exists or not in X

2019-04-03 05:16发布

how to find out if an attribute exists or not in XSL.

标签: xml xslt
3条回答
我命由我不由天
2楼-- · 2019-04-03 05:55
<xsl:choose>
   <xsl:when test="element/@attribute">
     do one thing
   </xsl:when>
   <xsl:otherwise>
     do something else
   </xsl:otherwise>
</xsl:choose>
查看更多
不美不萌又怎样
3楼-- · 2019-04-03 06:07
<xsl:value-of select="element[not(@attribute)]"/>

if need select some element without attribute

查看更多
太酷不给撩
4楼-- · 2019-04-03 06:16

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.

查看更多
登录 后发表回答