Re: [xsl] count with variables?

Subject: Re: [xsl] count with variables?
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 9 Aug 2001 17:55:01 +0100
Hi Resch,

> the nodes <fragmentX> don´t have allways the same name, but in node
> <status> and <sub-root> they have, so i read the name and put it
> into a variable called $actuall_name. that´s ok, and now i want to
> count these nodes under <sub-root> and give the result to an other
> varible called $actuall_result.

I'm a bit confused because the fragmentX elements that you show in the
sample XML are all children of sub-sub-root rather than sub-root.

Anyway, you need to find the elements that have the same name as is
held in the $actuall_name variable. If you're on the parent of these
elements, then you can find them by collecting all the elements and
filtering in those whose name() is equal to $actuall_name:

  *[name() = $actuall_name]

So I think you want either:

  <xsl:variable name="actuall_result"
                select="count(root/sub-root/sub-sub-root
                                /*[name() = $actuall_name])" />

or possibly:

  <xsl:variable name="actuall_result"
                select="count(root/sub-root//*[name() =
                                               $actuall_name])" />

> i´ve tried something like
> <xsl:value-of select="count(../..[$actuall_name])"/>  or
> <xsl:value-of select="count(root/sub-root[$actuall_name])"/>
> but nothing is correct. 

Just to clarify why these don't work. In both of these, you're using
$actuall_name directly in a predicate. Predicates filter in particular
nodes when their contents evaluates as true. If $actuall_name has any
characters in it (which it will do) then it will always evaluate as
true, so the predicate is effectively doing nothing and you may as
well be doing:

<xsl:value-of select="count(../..)" />
<xsl:value-of select="count(root/sub-root)" />

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