[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to test by nodename if node exsits and not emp

Subject: Re: 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
xsl if test nodename
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.