Re: [xsl] How do i check null values in a for each???

Subject: Re: [xsl] How do i check null values in a for each???
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 30 Apr 2002 13:14:42 +0100
Hi Suman,

> Please see the following snippet:
>
> <xsl:for-each select="attr[@name='addrdesc1' or @name='AddrDesc1']">
>     <td>&#160; <input type="text">
>      <xsl:attribute name="name">txtAddressDesc</xsl:attribute>
>      <xsl:attribute name="value"><xsl:value-of select="value"></xsl:value-of></xsl:attribute>
>      </input>
>          </td>
> </xsl:for-each>
>
> Now consider for instance the above for-each does'nt return a value
> at all. I.e there is'nt an attribute called addrdesc1 in the xml
> schema response So obviously it does't enter the for-each and does
> NOT display the text box.
>
> but i still want to display an empty text box to the user
> in the XSL , How do i do this???

If you try to select a node set, and test that node set, you'll get
'true' if the node set contains some nodes, and 'false' if it doesn't.
So in this case you can do:

  <xsl:variable name="attrs"
    select="attr[@name = 'addrdesc1' or @name = 'AddrDesc1']" />
  <xsl:choose>
    <xsl:when test="$attrs">
      <td>
        &#160;
        <input type="text" name="txtAddressDesc" />
      </td>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="$attrs">
        <td>
          &#160;
          <input type="text" name="txtAddressDesc" value="{value}" />
        </td>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>

If, as I suspect, there can only be one attr in this context whose
name attribute is either 'addrdesc1' or 'AddrDesc1', then it would be
simpler to write:

  <xsl:variable name="attr"
    select="attr[@name = 'addrdesc1' or @name = 'AddrDesc1']" />
  <td>
    &#160;
    <input type="text" name="txtAddressDesc">
      <xsl:if test="$attr">
        <xsl:attribute name="value">
          <xsl:value-of select="$attr/value" />
        </xsl:attribute>
      </xsl:if>
    </input>
  </td>

Although probably in this case simply doing the following would work
just as well:

  <td>
    &#160;
    <input type="text" name="txtAddressDesc"
           value="{attr[@name = 'addrdesc1' or
                        @name = 'AddrDesc1']/value}" />
  </td>

Cheers,

Jeni

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


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


Current Thread