Deborah Smith
Deborah Smith

Reputation: 41

xsl: when expression with boolean datatype

I am having a problem with an xsl:when statement using a boolean datatype. As you can see when I use a boolean expression with another datatype and use a boolean expression to test that the attribute "exists" (Phone/@Type), it works, but when I use a test...="'true'" expression or it's opposite I get either "no" for both records or "yes" for both records instead of 1 yes and 1 no. I've tried a number of variations on the test expression based on answers I found here. As part of the exercise I'm doing, the IsManager attribute is supposed to be boolean, not just the expression testing it.

Here's my xml:

 <Person IsManager="true">
  <FirstName>Alex</FirstName>
  <LastName>Chilton</LastName>
  <Phone Type="Cell">555-1212</Phone>
  <IM>Alex1092</IM>

And the second record:

<Person>
  <FirstName>Laura</FirstName>
  <LastName>Chilton</LastName>
  <Phone>555-5678</Phone>
  <IM>LaurethSulfate</IM>

Here's the xsl:

    <xsl:for-each select="//Person">
       <tr>
         <td><xsl:value-of select="FirstName"/></td>
         <td><xsl:value-of select="LastName"/></td>
        <xsl:choose> 
          <xsl:when test="boolean(Phone/@Type)">
             <td><xsl:value-of select="Phone/@Type"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td>Home</td>
          </xsl:otherwise>
    </xsl:choose>
          <td><xsl:value-of select="Phone"/></td> 
          <td><xsl:value-of select="IM"/></td>
        <xsl:choose>     
          <xsl:when test="(Person/@IsManager)='true'">
            <td>Yes</td>  
          </xsl:when>   
          <xsl:otherwise>
            <td>No</td>      
          </xsl:otherwise>
        </xsl:choose>
       </tr>
    </xsl:for-each>

And here's the result:

First Name Last Name Phone Type Phone IM Manager Alex Chilton Cell 555-1212 Alex1092 No Laura Chilton Home 555-5678 LaurethSulfate No

Any help would be appreciated. Thank you.

Upvotes: 2

Views: 7277

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

You're inside a for-each select="//Person" so the context node is a Person element. Therefore instead of

<xsl:when test="(Person/@IsManager)='true'">

you need just

<xsl:when test="@IsManager='true'">

Note that this is not comparing to a boolean as such, it's comparing the value of the IsManager attribute to the string "true".

Upvotes: 4

Related Questions