AabinGunz
AabinGunz

Reputation: 12347

How to identify using xsl whether a node exists in my xml?

I have this xml

<game>
 <genre>
  <action>...</action>
  <racing>...</racing>
 <price>
..
..
 </price>
</genre>
</game>

I want to check whether price node is present in the xml using xsl. how can I do that? if price node is present then call a particular template else call another template

Upvotes: 1

Views: 808

Answers (1)

Osiris76
Osiris76

Reputation: 1244

You could simply call xsl:apply-templates with the match attribute set to the element name. If the element exists the template is called. It it does not exist, the template won't be called. If you are trying to build a if-else statement you could check the existence like this

<xsl:choose>
    <xsl:when test="boolean(price)">
        <!-- do something -->
    </xsl:when>
    <xsl:otherwise>
        <!-- do something else -->
    </xsl:otherwise>
</xsl:choose>

So you can check for existence of an element and react accordingly.

Upvotes: 2

Related Questions