Re: [xsl] How to test by nodename if node exsits and not empty

Subject: Re: [xsl] How to test by nodename if node exsits and not empty
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 1 Aug 2002 11:49:33 +0100
Hi René,

> I try to do this: pass a nodename of a childnode of the current node
> as a parameter to a template and test if it exists and, if so, if it
> has a contents and/or attributes.
>
> My problem is that I can't get the the expression right to count the
> number of attributes of $Node. For example count(@*) returns the
> number of attributes for the current node and I've tried
> count($Node/@*), count($Node[@*]), count($Node/*[@*]) and so on, but
> the parser complains that the "expression must evaluate to a
> node-set".
>
> If I don't count the attributes, <aNode xyz="someinfo"/> would be
> considered empty, which I don't want.
>
> WHAT DO I DO WRONG????

Here:

>     <xsl:variable name="Node">
>         <xsl:copy-of select="./*[local-name()=$NodeName]"/>
>     </xsl:variable>

you're assigning to the variable $Node a result tree fragment
containing a copy of the child element of the current node whose name
is equal to $NodeName.

What you want to do is have $Node actually contain itself the child
element of the current node whose name is equal to $NodeName. So use
the select attribute instead:

  <xsl:variable name="$Node"
                select="*[local-name() = $NodeName]" />

and then do what you were trying to do before:

  <xsl:if test="not($Node) or
                (not(normalize-space($Node)) and not($Node/@*))">
    ...
  </xsl:if>

BTW, it's better to just test whether there are any attributes on
$Node with simply not($Node/@*) rather than count($Node/@*) = 0 since
with the former a most processors can know to stop once they find the
first attribute whereas with the latter, it takes a bit of a smarter
processor to realise that the count() can only be 0 if there are no
attributes. Plus the former is shorter!

Cheers,

Jeni

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


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


Current Thread