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

RE: Count all instances of a named element in a tree

Subject: RE: Count all instances of a named element in a tree
From: Emmanuel Bégué <eb@xxxxxxxxxx>
Date: Fri, 29 May 2009 18:02:31 +0200
RE:  Count all instances of a named element in a tree
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@xxxxxxxxxx]
> Sent: Friday, May 29, 2009 5:49 PM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  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

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.