xsl-list
[Top] [All Lists]

RE: [xsl] Count all instances of a named element in a tree

2009-05-29 12:04:38
Hello,

If you don't want to use the '//' construct, you can use keys;
for example:

  <xsl:key name="nodes" match="countmein" use="'x'"/>
  <xsl:template match="/">
    <result countmein="{count(key('nodes','x'))}"/>
    </xsl:template>

Resulting document:
  <result countmein="8"/>

Regards,
EB

-----Original Message-----
From: W Charlton [mailto:XSLList(_at_)ymonda(_dot_)net]
Sent: Friday, May 29, 2009 5:49 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Count all instances of a named element in a tree



Does anyone know an elegant solution to counting all elements in an XML
fragment where the elements to be counted are nested in a tree? An example
of the XML is below:

   <xmltree>
      <countmein>Node1</countmein>
      <nodes>
         <node>
            <countmein>Node2</countmein>
            <level>1</level>
            <nodes>
               <node>
                  <countmein>Node3</countmein>
                  <level>2</level>
                  <nodes>
                     <node>
                        <countmein>Node4</countmein>
                        <level>3</level>
                        <nodes/>
                    </node>
                 </nodes>
              </node>
              <node>
                 <countmein>Node5</countmein>
                 <level>2</level>
                 <nodes/>
              </node>
              <node>
                 <countmein>Node6</countmein>
                 <level>2</level>
                 <nodes/>
              </node>
           </nodes>
        </node>
        <node>
           <countmein>Node7</countmein>
           <level>1</level>
           <nodes>
              <node>
                 <countmein>Node8</countmein>
                 <level>2</level>
                 <nodes/>
              </node>
           </nodes>
        </node>
     </nodes>
   </xmltree>

The count in this case would be 8.

I am using XSLT 1.0

Thanks in advance.


William Charlton
The yMonda team
yMonda Limited
w: www.ymonda.net



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--