Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

XML radio buttons

I have a problem in displaying the value of radio button in XSL.
My code goes like:
************XML************
<test>
<radio1>True</radio1>
<radio2>False</radio2>
</test>
***********XSL FILE***********
<xsl:template match="/">
<input type="radio" name="r1">
<input type="radio" name="r2">
<xsl:attribute name="checked">
<xsl:value-of select="radio1"/>
<xsl:value-of select="radio2"/>
</xsl:attribute>
</input>
</input>
</xsl:template>
The question is if the value of radio buttons is true/false it shod take
the value into the radio buttons.
Any help appreciated
[739 byte] By [vani] at [2007-11-9 14:55:06]
# 1 Re: XML radio buttons
vani,
ur xml (don't mind) doesn't look good in first place
it shud be

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="radio.xsl"?>
<radios>
<radio>true</radio>
<radio>false</radio>
</radios>

Though the MSDN docs say, <INPUT type="radio" checked=false/true> is the
syntax, but I've seen "checked" attrib doesn't contain any value in Tag declaration.
So only <Input type="radio" checked> will work.

In ur case, u have xml data as the decision parameter for the checked/unchecked
nature (true/false). so u can try this

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" language="VBScript">
<xsl:template match="/">
<HTML>
<BODY>
<xsl:for-each select="radios/radio">
<input type="radio">
<xsl:if expr="me.nodeTypedValue">
<xsl:attribute name="checked" />
</xsl:if>
</input>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

this works.
rohit
Rohit Wason at 2007-11-11 23:33:57 >
# 2 Re: XML radio buttons
"vani" <choclatecake@hotmail.com> wrote:
>
>I have a problem in displaying the value of radio button in XSL.
>My code goes like:
>************XML************
><test>
><radio1>True</radio1>
><radio2>False</radio2>
></test>
>***********XSL FILE***********
><xsl:template match="/">
><input type="radio" name="r1">
><input type="radio" name="r2">
><xsl:attribute name="checked">
><xsl:value-of select="radio1"/>
><xsl:value-of select="radio2"/>
></xsl:attribute>
></input>
></input>
></xsl:template>
>The question is if the value of radio buttons is true/false it shod take
>the value into the radio buttons.
>Any help appreciated

I'm not up to speed on the way radio buttons work in HTML but treating it
as a pure XSL problem the following seems to work:

XML: as above

***********XSL FILE***********

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="radio1">
<input type="radio" name="r1">
<xsl:attribute name="checked">
<xsl:value-of select="."/>
</xsl:attribute>
</input>
</xsl:template>

<xsl:template match="radio2">
<input type="radio" name="r2">
<xsl:attribute name="checked">
<xsl:value-of select="."/>
</xsl:attribute>
</input>
</xsl:template>

</xsl:stylesheet>

I'm new to XSL so I'm not sure how stylistically elegant this is. However
it seems to do the job, I hope it helps.
Greg at 2007-11-11 23:34:54 >
# 3 Re: XML radio buttons
"vani" <choclatecake@hotmail.com> wrote:
>
>I have a problem in displaying the value of radio button in XSL.
>My code goes like:
>************XML************
><test>
><radio1>True</radio1>
><radio2>False</radio2>
></test>
>***********XSL FILE***********
><xsl:template match="/">
><input type="radio" name="r1">
><input type="radio" name="r2">
><xsl:attribute name="checked">
><xsl:value-of select="radio1"/>
><xsl:value-of select="radio2"/>
></xsl:attribute>
></input>
></input>
></xsl:template>
>The question is if the value of radio buttons is true/false it shod take
>the value into the radio buttons.
>Any help appreciated

Vani:

On working with XML given by Rohit You can also use thefollowing XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" language="VBScript">
<xsl:template match="/">
<HTML>
<BODY>
<xsl:for-each select="radios/radio">
<input type="radio">
<xsl:attribute name="checked" />
<xsl:value-of select='.'/>
</xsl:attribute>
</input>
</xsl:for-each>

I hope it will be a good alternate.
Manish.
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
Manish Gupta at 2007-11-11 23:35:55 >
# 4 Re: XML radio buttons
Thanks Rohit!Ur solution did work

"Rohit Wason" <rohitw@futuresoftindia.com> wrote:
>
>vani,
>ur xml (don't mind) doesn't look good in first place
>it shud be
>
><?xml version="1.0"?>
><?xml-stylesheet type="text/xsl" href="radio.xsl"?>
><radios>
><radio>true</radio>
><radio>false</radio>
></radios>
>
>Though the MSDN docs say, <INPUT type="radio" checked=false/true> is the
>syntax, but I've seen "checked" attrib doesn't contain any value in Tag
declaration.
>So only <Input type="radio" checked> will work.
>
>In ur case, u have xml data as the decision parameter for the checked/unchecked
>nature (true/false). so u can try this
>
><xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" language="VBScript">
><xsl:template match="/">
> <HTML>
> <BODY>
> <xsl:for-each select="radios/radio">
> <input type="radio">
> <xsl:if expr="me.nodeTypedValue">
> <xsl:attribute name="checked" />
> </xsl:if>
> </input>
> </xsl:for-each>
> </BODY>
> </HTML>
></xsl:template>
></xsl:stylesheet>
>
>this works.
>rohit
Vani at 2007-11-11 23:36:52 >