[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] How to test if a named attribute exists.


Subject: Re: [xsl] How to test if a named attribute exists.
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 10 Mar 2003 17:08:52 +0000

Hi Imrran,

> I would like to do a test to determine if a named attribute exists
> in one or more of the child elements.
>
> If the attributes were elements, I can do this with a count of all
> the named elements below the <parent> element. For example if I have
> the named elements in a lookup table, I can use the following to
> check for the existence of each of the elements using the element's
> name from the lookup table as follows:
>
>   <xsl:for-each select="$lookup-node/*">
>     <xsl:variable name="ItemName"
> select="string(name(.))"/>
>     <xsl:if
> test="count($parent-node//*[name(.)=$ItemName]) &gt;
> 0">
>       <th><xsl:value-of select="$ItemName"/></th>
>     </xsl:if>
>   </xsl:for-each>
>
> But I do not know how to do the same in the case of attributes.

I'm not sure what $lookup-node or $parent-node are being set to, but I
think that just putting '@' in front of the '*' so that you select
attributes rather than elements might do the trick:

  <xsl:for-each select="$lookup-node/@*">
    <xsl:variable name="ItemName" select="string(name(.))"/>
    <xsl:if test="count($parent-node//@*[name(.)=$ItemName]) &gt; 0">
      <th><xsl:value-of select="$ItemName"/></th>
    </xsl:if>
  </xsl:for-each>

By the way, you can replace the "count(...) > 0" construct path with
the expression that selects the node set: if there aren't any nodes in
the node set then the test will return false, otherwise it will return
true. So you could use:

  <xsl:for-each select="$lookup-node/@*">
    <xsl:variable name="ItemName" select="string(name(.))"/>
    <xsl:if test="$parent-node//@*[name(.)=$ItemName]">
      <th><xsl:value-of select="$ItemName"/></th>
    </xsl:if>
  </xsl:for-each>

Cheers,

Jeni

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


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



Current Thread