tags:

views:

149

answers:

2

how to check the tag exists and the value is 'On' do something in xsl

please correct me.,

<xsl:if test="$status and $status='On'">

 //do something

</xsl:if>

can we skip checking whether the tag exists and direclty check for the value.

<xsl:if test="$status='On'">

     //do something

    </xsl:if>

is it a correct practice.,

+2  A: 

you should use xpath expressions

<xsl:if test="/path/node = 'On'">

</xsl:if>

or is $status a xsl param?

Ivo
yes $status is a xml param
flash
then <xsl:if test="$status='On'"> //do something </xsl:if> is correct
Ivo
is it not necessary to check whether 'status' exists using '$status'
flash
correct, thats not needed. you wont get any null exeception or something in xslt
Ivo
+2  A: 

<xsl:if test="$status and $status='On'">

The above is redundant, because if $status='On' then the boolean value of $status is true.

Therefore, the expression contained in the @test attribute of the above xslt instruction is equivalent to just: $status='On', which is shorter.

This completely answers the question.

It seems to me that you want to test if $status is defined and then test for its value. This is not correct -- if a reference is made to an undefined xsl:variable, this causes an error as per the W3 XSLT specification.

Dimitre Novatchev
@flash: Your question "to check the tag exists and the value is 'On'" could also imply that `$status` is the *name of an element* you want to check, not an actual string value. If this is the case, use `<xsl:if test="*[name() = $status and .='On']">`.
Tomalak