xmlxsltradio-buttonchecked

How to check radio buttons based upon returned XML data using an XSLT


Hey I'm pretty new to XSLT's and trying to get my head around using them to produce and populate a form. I have made the XSLT and can get the XML data from the server and I think I have correctly matched up all the fields etc with their respective XML data. However I can't work out how to make radio buttons and checkboxes checked depending on the stored XML data. I have found a couple of similar posts on the net but I cannot make them function correctly and was hoping someone on here might be able to give me a hand.

I have made some small test code with which to try and get this working as I didn't want to risk messing up the full form. The test XML and XSL are shown below.

XML

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test2.xsl"?>
<root>
<radiobuttons>
    <radio1>Y</radio1>
    <blurb>blahblahblah</blurb>
</radiobuttons>
</root>

XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" omit-xml-declaration="no"
            encoding="UTF-8"/>
<xsl:template match="/">
<HTML>
<BODY>
<form>
<xsl:apply-templates select="root"/>
</form>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="root">

<input type="radio" name="radio1" value="Y" >
<xsl:if test="root/radiobuttons/radio1='Y'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Radio Button 1

<input type="radio" name="radio1" value="N" >
<xsl:if test="root/radiobuttons/radio1='N'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Radio Button 2

<br/>
<input name="blurb" type="text" id="blurb" value="{./radiotbuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>

I'd like the first radio button to be checked in the resulting HTML if the XML has a value of Y stored and the 2nd buton if it has a value of N. If anyone can shed some light on why this isn't working or if it is completely the wrong approach then give me a correct example I'd be really grateful.


Solution

  • you have made an error in your xpath

    u must remove root/ from "root/radiobuttons/radio1" as at this point you are inside the root node so doesnt really exsist anymore :P

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes" omit-xml-declaration="no"
                encoding="UTF-8"/>
    <xsl:template match="/">
    <HTML>
    <BODY>
    <form>
    <xsl:apply-templates select="root"/>
    </form>
    </BODY>
    </HTML>
    </xsl:template>
    
    <xsl:template match="root">
    
    <input type="radio" name="radio1" value="Y" >
    <xsl:if test="radiobuttons/radio1='Y'">
    <xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
    </input>Radio Button 1
    
    <input type="radio" name="radio1" value="N" >
    <xsl:if test="radiobuttons/radio1='N'">
    <xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
    </input>Radio Button 2
    
    <br/>
    <input name="blurb" type="text" id="blurb" value="{./radiobuttons/blurb}"></input>
    </xsl:template>
    </xsl:stylesheet>