Re: Adding a chekbox with XSL

Subject: Re: Adding a chekbox with XSL
From: Dylan Parker <dylan@xxxxxxxxxxxxx>
Date: Mon, 2 Oct 2000 17:33:33 -0700
Monday, October 02, 2000, 2:47:43 PM, you wrote:

> I have an XML document that looks like this:

> <DemoMenu>
>   <option strValue="Setup" textValue="Y"/>
> </ndsDemoMenu>

> If the value of DemoMenu.option.textValue == 'Y' then I want to add a
> checkbox to my page that is already checked.  If the value is not == to
> 'Y' then I want it to be unchecked.  Setup is the name of the checkbox.

> I've figure out how to extract and display "Setup", what I can't figure
> out is how to use xsl:if (I realize xsl:choose is really what I want but
> I wanted to simplify until I get the basics figured out) to verify the
> value of textValue.  I tried:

>     <xsl:if test="&lt;xsl:value-of select=&quot;@textValue&quot;/&gt; =
&quot;Y&quot;">>
>             <p align="center"><xsl:value-of select="text"/><input
> Type="Checkbox" align="center" checked="Y" value="Y"/></p>
>     </xsl:if>
> trying to say
>     <xsl:if test="<xsl:value-of select="@textValue"/> = "Y"">
>             <p align="center"><xsl:value-of select="text"/><input
> Type="Checkbox" align="center" checked="Y" value="Y"/></p>
>     </xsl:if>

You can't put elements (eg. xsl:value-of) inside of an attribute.. in this case
the test attribute of the element xsl:if.

But, don't worry! because there is an easier way to get at that value.

When you say "setup" is the "name" of the checkbox.. I will assume you mean the
text that resides beside it since that is what it appears you are trying to do.

This should work :
<p align="center">
   <xsl:value-of select="strValue"/>
   <xsl:if test="@textValue='Y'">  <!-- checked -->
           <input type="checkbox" align="center" checked="Y" value="Y"/>
   </xsl:if>
   <xsl:if test="not(@textValue='Y')">  <!-- not checked -->
           <input type="checkbox" align="center" value="Y"/>
   </xsl:if>
</p>

I don't know if you still wanted the value of the checkbox etc to be the same in
each case.. but the general structure is there. Notice I extracted the
duplicated sections and just put them once on the outside of the xsl:if
sections. You could even reduce further by just adding an attribute named
checked="" in the case where it should be checked. But maybe that is getting
silly =)

Note.. the above DOES assume that it is being executed while within the option
node... that is, within an <xsl:apply-template select="option"> or something
similar.

You could also just use xsl:choose like this :

<p align="center">
   <xsl:value-of select="strValue"/>
   <xsl:choose>
      <xsl:when test="@textValue='Y'">  <!-- checked -->
          <input type="checkbox" align="center" checked="Y" value="Y"/>
      </xsl:when>
      <xsl:otherwise>  <!-- not checked -->
           <input type="checkbox" align="center" value="Y"/>
      </xsl:otherwise>
   </xsl:choose>
</p>


And although a little wordier... it does seem clearer to me what is going on.

Good luck.
Dylan Parker



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • Adding a chekbox with XSL
    • Lewis - Mon, 02 Oct 2000 17:47:43 -0400
      • Dylan Parker - Mon, 2 Oct 2000 17:33:33 -0700 <=
      • Keith - Tue, 3 Oct 2000 08:17:51 -0500
      • <Possible follow-ups>
      • Lewis - Tue, 03 Oct 2000 14:55:13 -0400
      • Lewis - Tue, 03 Oct 2000 15:19:11 -0400