xsl-list
[Top] [All Lists]

Re: counting specific nodes

2003-10-15 18:58:20
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(_at_)hotmail(_dot_)com> 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



<Prev in Thread] Current Thread [Next in Thread>