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

Re: counting specific nodes

Subject: Re: counting specific nodes
From: Mukul Gandhi <mukulgw3@xxxxxxxxx>
Date: Wed, 15 Oct 2003 18:58:20 -0700 (PDT)
counting xml nodes
Here is another solution --

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan">
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>
<xsl:variable name="startpos">1</xsl:variable>
	
<xsl:template match="/vs">
   <xsl:variable name="dots">
      <xsl:for-each select="ve">
	<xsl:variable name="val"
select="substring(@pos,1,1)"/>
	<xsl:if test="($val = $startpos) or ($val &gt;
$startpos)">
	    <xsl:call-template name="countdots">	     
<xsl:with-param name="stringparam" select="@pos"/>
	      <xsl:with-param name="noofdots" select="0"/>
	    </xsl:call-template>
	</xsl:if>
     </xsl:for-each>
   </xsl:variable>
		
   Answer - <xsl:value-of
select="count(xalan:nodeset($dots)/a)" />
</xsl:template>
	
<xsl:template name="countdots">
  <xsl:param name="stringparam"/>
  <xsl:param name="noofdots"/>
  
  <xsl:variable name="sbst"
select="substring-after($stringparam, '.')"/>
  <xsl:if test="$sbst != '' ">
     <xsl:call-template name="countdots">
	<xsl:with-param name="stringparam" select="$sbst"/>
	<xsl:with-param name="noofdots" select="$noofdots +
1"/>
     </xsl:call-template>
  </xsl:if>
  <xsl:if test="$sbst = '' ">
     <xsl:if test="$noofdots = 3">
	<a/>
     </xsl:if>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

At the begining of XSL, a varibale is declared to
mention the starting position; <xsl:variable
name="startpos">1</xsl:variable>

The XSL uses recursion to find the count of the
desired nodes. The above XSL creates a tag <a/> each
time it encounters a node in original XML having 3
dots (whose position is, on or after the "startpos"
value node). A variable "dots" ( declared as --
<xsl:variable name="dots"> )holds these <a/> tags
(which is a RTF -- result tree fragment). 

I convert the RTF into a nodeset, and calculate the
count of <a/> tags, *which is the answer*.

Regards,
Mukul


--- james walker <jameswalkerandy@xxxxxxxxxxx> wrote:
> given the xml
> <vs>
> <ve pos="1"></ve>
> <ve pos="1.1"></ve>
> <ve pos="1.1.1"></ve>
> <ve pos="1.1.1.1"></ve>
> <ve pos="1.1.1.2"></ve>
> <ve pos="1.1.1.3"></ve>
> <ve pos"1.2"></ve>
> <ve pos="1.2.1"></ve>
> <<ve pos="1.2.1.1"></ve>
> <ve pos="2"></ve>
> <ve pos="2.1"></ve>
> <ve pos="2.1.1"></ve>
> <ve pos="2.1.1.1"></ve>
> </vs>
> 
> and given that i am starting on a node with pos=1
> (or 2 or 3....), how do i 
> count the nodes which have position with 3 dots only
> (e.g. pos=1.1.2.1?) and 
> start with the current node position. I came up with
> something like this:
> 
> <xsl:variable name="rowspan"
> select="count(ve[starts-with(@position, 
> current()/@position) and contains(...........)])" />
> e.g. for position 1, it should be 4 (1.1.1.1 ,
> 1.1.1.2, 1.1.1.3, 1.2.1.1)
> 
>
_________________________________________________________________
> Get Hotmail on your mobile phone
> http://www.msn.co.uk/msnmobile
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.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.