Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if XML node exists in XSLT

Tags:

xml

xslt

xmlnode

Is there a better way of finding if XML node exists (in XSLT) rather than using:

<xsl:choose>
  <xsl:when test="...........">body node exists</xsl:when>
  <xsl:otherwise>body node missing</xsl:otherwise>
</xsl:choose>
like image 568
user3767641 Avatar asked May 03 '16 22:05

user3767641


1 Answers

Alternatives to xsl:choose

Define better; xsl:choose covers conditional expression quite well. Being better requires measurement against some criteria, and none were provided. Nevertheless, here are some alternatives which you can assess as you see fit:

XSLT 1.0

<xsl:if test="/path/to/node">node exists</xsl:if>
<xsl:if test="not(/path/to/node)">node missing</xsl:if>

XSLT 2.0

<xsl:value-of select="if (/path/to/node) then 'node exists' else 'node missing'"/>
like image 85
kjhughes Avatar answered Nov 01 '22 16:11

kjhughes