Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt:if how to test for boolean values

Tags:

xslt

I cannot figure out a way to check if the value of an element is "true" or "false".

It's boolean, converted from a dataset. When I select the value, I see it is either "true" or "false", but my test does not get the expected behaviour. (it is always false.) I tried almost everything, this is my first xslt application, so help is appreciated.

      <xsl:if test="ispassive">
        <tr>
          <td>
            <em>pasif değil</em>
            <hr></hr>
          </td>
        </tr>
      </xsl:if>
like image 805
hazimdikenli Avatar asked Jan 13 '10 20:01

hazimdikenli


3 Answers

After posting here I realized that my <xsl:if> test was outside my <xsl:for-each> loop.

After repositioning the <xsl:if> conditions, I found out that this works for me.

        <xsl:if test="ispassive='true'">
          <tr>
            <td>
              <em>pasif</em>
              <hr></hr>
            </td>
          </tr>
        </xsl:if>

Comparing the evaluated string value of the ispassive element to 'true' works fine, (which I had tried before, but because I misplaced it in the xslt, it was always failing because it failed to select the ispassive element to evaluate it's value.

like image 112
hazimdikenli Avatar answered Nov 13 '22 09:11

hazimdikenli


Depending how you represent your booleans it could be a lot of things, what does your data look like?

Most likely scenario, false is represented as 0 and true could be any non 0 value.

<xsl:if test="$myvalue = 0">
    -- output if false --
</xsl:if>

but you're likely better of using xsl:choose

<xsl:choose>
    <xsl:when test="$myvalue = 0">
        -- output if false --
    </xsl:when>
    <xsl:otherwise>
        -- output if true --
    </xsl:otherwise>
</xsl:choose>

You can leave out the otherwise, if you don't need it.

like image 16
Kris Avatar answered Nov 13 '22 10:11

Kris


This xslt is looking for a node in the current context called ispassive.

So if your xml is

<Root>
   <ispassive />
</Root>

You'll get true. Generally, you should specify an xpath to the value you want to check. So if your xml is

<Root>
      <Node ispassive="true"/>
</Root>

and you replace

<xsl:if test="ispassive">

with

<xsl:if test="//@ispassive = 'true'">

Your stylesheet will work as expected.

like image 11
Robert Christie Avatar answered Nov 13 '22 11:11

Robert Christie