Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

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

like image 432
Jawed Avatar asked Feb 07 '11 07:02

Jawed


3 Answers

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.

like image 177
Dimitre Novatchev Avatar answered Nov 15 '22 17:11

Dimitre Novatchev


<xsl:choose>
   <xsl:when test="element/@attribute">
     do one thing
   </xsl:when>
   <xsl:otherwise>
     do something else
   </xsl:otherwise>
</xsl:choose>
like image 28
Arsen Mkrtchyan Avatar answered Nov 15 '22 17:11

Arsen Mkrtchyan


<xsl:value-of select="element[not(@attribute)]"/>

if need select some element without attribute

like image 1
Oleg Motorin Avatar answered Nov 15 '22 15:11

Oleg Motorin