Re: [xsl] how to change the xsl text field length?

Subject: Re: [xsl] how to change the xsl text field length?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 12 Nov 2001 10:51:06 +0000
Hi,

> My xml looks like
>
> <FIELD><FIELD_NAME>Sysout Class</FIELD_NAME>
> <FIELD_VALUE>A</FIELD_VALUE><FIELD_LENGTH>1</FIELD_LENGTH></FIELD>
>
> my relevant xsl looks like
>
> <xsl:for-each select="message/text/FIELD"> <tr><td><xsl:value-of
> select="FIELD_NAME"/></td><td><input type="Text"><xsl:attribute
> name="name"><xsl:value-of
> select="FIELD_NAME"/></xsl:attribute><xsl:attribute
> name="value"><xsl:value-of
> select="FIELD_VALUE"/></xsl:attribute></input></td> </tr></xsl:for-each>
> now the xsl produces a field name and an editable text box showing the field
> value.
>
> I want the length of the field box to be equal to the <FIELD_LENGTH>
> variable. This might be something really simple.

Indeed it is. If you take a look at the HTML Rec
(http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT), you'll
see that there are 'size' and 'maxlength' attributes on input elements
that allow you to set the size and maximum length of the input fields
that are generated.

Assuming that you want the size and maxlength to both equal the value
of the FIELD_LENGTH element, you need either:

  <xsl:for-each select="message/text/FIELD">
    <tr>
      <td><xsl:value-of select="FIELD_NAME"/></td>
      <td>
        <input type="text">
          <xsl:attribute name="name">
            <xsl:value-of select="FIELD_NAME"/>
          </xsl:attribute>
          <xsl:attribute name="value">
            <xsl:value-of select="FIELD_VALUE"/>
          </xsl:attribute>
          <xsl:attribute name="size">
            <xsl:value-of select="FIELD_LENGTH"/>
          </xsl:attribute>
          <xsl:attribute name="maxlength">
            <xsl:value-of select="FIELD_LENGTH"/>
          </xsl:attribute>
        </input>
      </td>
    </tr>
  </xsl:for-each>

Or, if you don't like typing:

  <xsl:for-each select="message/text/FIELD">
    <tr>
      <td><xsl:value-of select="FIELD_NAME"/></td>
      <td>
        <input type="text" name="{FIELD_NAME}" value="{FIELD_VALUE}"
               size="{FIELD_LENGTH}" maxlength="{FIELD_LENGTH}" />
      </td>
    </tr>
  </xsl:for-each>

The above uses "attribute value templates" - anything in {}s within an
attribute value gets evaluated and is inserted into the attribute
value. As you can see, it's a lot more concise than using
xsl:attribute to add simple attributes to elements.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread