Stedman
Stedman

Reputation: 87

Display Yes/No based on returned value of attribute

I am brand new to XML/XSL (like 2 days new). I have a line where I am doing an xsl:value-of select which returns a True/False attribute. I would like to have it display Yes/No instead, but I have been unsuccessful it attempting to do this. Below is the line I have currently. Could someone please tell me what I need to add to make it display Yes or No?

<fo:block>Automatic Sprinklers Required: &#xA0;
      <xsl:value-of select="Attributes/AttributeInstance/Attribute[@id='1344297']/../Value"/>
</fo:block>

Upvotes: 2

Views: 3095

Answers (2)

Anshu
Anshu

Reputation: 7853

That should be pretty straight forward, following is my XML :

<form>
    <value>False</value>
</form>

And my XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:date="http://exslt.org/dates-and-times" xmlns:str="http://exslt.org/strings">
    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="/form/value = 'True'">
                <xsl:text>Yes</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>No</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Koby Mizrahy
Koby Mizrahy

Reputation: 1371

Use xsl:choose block to test the value. The choose has the following syntax:

<xsl:choose>
   <xsl:when test="some Boolean condition">
    <!-- "if" stuff -->
  </xsl:when>
  <xsl:otherwise>
    <!-- "else" stuff -->
  </xsl:otherwise>
</xsl:choose>

I reformatted your code to call a template that does this test and print Yes/No according to the Boolean value:

<fo:block>Automatic Sprinklers Required: &#xA0;
    <xsl:call-template name="formatBoolean">
       <xsl:with-param name="theBoolValue" select="Attributes/AttributeInstance/Attribute[@id='1344297']/../Value"/>
    </xsl:call-template>
</fo:block>


<xsl:template name="formatBoolean">
   <xsl:param name="theBoolValue"/>

   <xsl:choose>
       <xsl:when test="$theBoolValue = true()">
          Yes
       </xsl:when>
       <xsl:otherwise>
          No
       </xsl:otherwise>
   </xsl:choose>
</xsl:template>

This code should work although I didn't tested it to see if it has any syntax error.

Good luck!

Koby

Upvotes: 4

Related Questions