Re: [xsl] How define the property "checked" in radio box with xsl?

Subject: Re: [xsl] How define the property "checked" in radio box with xsl?
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Wed, 20 Nov 2002 15:44:42 +0100
> When I have a radio's input type is the value of attribute "estado"
> in XML is "checked" I want that radio checked. The HTML produced must
>  be like this:
>
> <input type="radio" name="tiporetorno" checked/>
>
> The problem is: How I create that?! It can't be an attribute! It's
> only a property. Can you help me?

Hello Hélder,

that's not quite correct. You must write it as attribute in XML. The serializer will change it to the simplified attribute without a value. So simply use <xsl:attribute name="checked">checked</xsl:attribute>.


So far the solution of your problem. Now your code:


Please try to use templates, because this would make your code much more
readable. Furthermore you don't need to use <xsl:element> and
<xsl:attribute>, it's shorter to use literal result elements and
attribute value templates. May I rewrite your code?

<xsl:template match="form">
  <form>
    <xsl:apply-templates select="action/@*"/>
    <table cellspacing="0" cellpadding="0" border="0">
      <xsl:apply-templates select="fields/field"/>
    </table>
  </form>
</xsl:template>

<xsl:template match="action/@pagina">
  <xsl:attribute name="action">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="action/@method">
  <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="action/@name">
  <xsl:attribute name="ID">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="field[@type='submit']">
  <tr height="70px">
    <td colspan="3" align="center">
      <input class="inputButton">
        <xsl:copy-of select="@type | @name | @value"/>
      </input>
    </td>
  </tr>
</xsl:template>

<xsl:template match="field[@type='button']">
  <tr height="70px">
    <td colspan="3" align="center">
      <input class="inputButton" onclick="{@funcaoSubmit}">
        <xsl:copy-of select="@type | @name | @value"/>
      </input>
    </td>
  </tr>
</xsl:template>

<xsl:template match="field[@type='text']">
  <tr height="30px">
    <td width="150px" align="right">
      <a class="caption"><xsl:value-of select="@caption"/></a>
    </td>
    <td width="20px"></td>
    <td width="150px">
      <input class="inputText">
        <xsl:copy-of select="@type | @name | @value"/>
        <xsl:apply-templates select="@tamanho | @maxchar"/>
      </input>
      <xsl:if test="@value='--data--'">
        <script>
getDataField(document.all.item('<xsl:value-of select="@name"/>'));
        </script>
      </xsl:if>
    </td>
  </tr>
</xsl:template>

<xsl:template match="field[@type='radio']">
  <tr height="30px">
    <td width="150px" align="right">
      <a class="caption"><xsl:value-of select="@caption"/></a>
    </td>
    <td width="20px"></td>
    <td width="150px">
      <input>
        <xsl:copy-of select="@type | @name | @value"/>
        <xsl:if test="status='checked'">
          <xsl:attribute name="checked">checked</xsl:attribute>
        </xsl:if>
      </input>
    </td>
  </tr>
</xsl:template>

<xsl:template match="field[@type='hidden']">
  <input>
    <xsl:copy-of select="@type | @name | @value"/>
  </input>
</xsl:template>

<xsl:template match="field[@type='nota']">
  <tr>
    <td colspan="3" class="nota">
      <xsl:value-of select="@value"/>
    </td>
  </tr>
</xsl:template>

<xsl:template match="field/@tamanho">
  <xsl:attribute name="size">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="field/@maxchar">
  <xsl:attribute name="maxlength">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

Surely there can be optimized some things more. So for example are the templates for "submit" and "button" almost equal. But the above shell give you only some hints for better coding of XSLT. Furthermore this code is much more reusable. I hope I did no mistakes ;-)

Regards,

Joerg


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



Current Thread