xsl-list
[Top] [All Lists]

RE: Count preceeding sibling but in different element

2005-08-07 16:13:58
There is no function called preceeding. If there were one, it would be spelt
"preceding". There are axes named "preceding" and "preceding-sibling", but
axes are not functions.

Michael Kay
http://www.saxonica.com/
 

-----Original Message-----
From: Taco Fleur (@DataBroker) [mailto:taco(_at_)shelco(_dot_)com(_dot_)au] 
Sent: 07 August 2005 23:45
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Count preceeding sibling but in different element

Thanks guys, 
It appears there is a function called "preceeding" Which does 
the trick
nicely.
thanks 

-----Original Message-----
From: Mukul Gandhi [mailto:gandhi(_dot_)mukul(_at_)gmail(_dot_)com] 
Sent: Friday, 5 August 2005 4:48 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Count preceeding sibling but in different element

Please try this XSL..

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
 
<xsl:output method="xml" indent="yes" /> 
  
<xsl:template match="/element1">
   <element1>
     <xsl:for-each select="*">
       <xsl:copy>
         <xsl:call-template name="numberingTemplate">
           <xsl:with-param name="node-set" select="*" />
         </xsl:call-template>
       </xsl:copy>
     </xsl:for-each>
   </element1>
</xsl:template>
 
<xsl:template name="numberingTemplate">
   <xsl:param name="node-set" />
   
   <xsl:for-each select="$node-set">
     <xsl:element name="{name()}">
       <xsl:attribute name="number"><xsl:value-of
select="count(self::* | preceding::step)" /></xsl:attribute>
     </xsl:element>
   </xsl:for-each>
</xsl:template>
 
</xsl:stylesheet> 

For e.g. when it is applied to XML -
<element1>
 <element2>
    <step/>
    <step/>
 </element2>
 <element3>
    <step/>
 </element3>
 <element4>
    <step/>
    <step/>
    <step/>
 </element4>
</element1>

The output produced is -
<?xml version="1.0" encoding="UTF-8"?>
<element1>
   <element2>
      <step number="1"/>
      <step number="2"/>
   </element2>
   <element3>
      <step number="3"/>
   </element3>
   <element4>
      <step number="4"/>
      <step number="5"/>
      <step number="6"/>
   </element4>
</element1>

Regards,
Mukul


On 8/5/05, Taco Fleur <taco(_at_)shelco(_dot_)com(_dot_)au> wrote:
I am wanting to count all preceeding sibling (steps) but 
they could be 
in different elements

<element1>
 <element2>
    <step/>
    <step/>
 </element2>
 <element3>
    <step/>
 </element3>
 <element4>
    <step/>
    <step/>
    <step/>
 </element4>
</element1>

The output I am after is

<element1>
 <element2>
    <step number="1" />
    <step number="2"/>
 </element2>
 <element3>
    <step number="3"/>
 </element3>
 <element4>
    <step number="4"/>
    <step number="5"/>
    <step number="6"/>
 </element4>
</element1>

I have have the following which works fine on every step within an 
element, i.e.

 <element3>
    <step/>
 </element3>
 <element4>
    <step/>
    <step/>
    <step/>
 </element4>

Would become

 <element3>
    <step number="1"/>
 </element3>
 <element4>
    <step number="1"/>
    <step number="2"/>
    <step number="3"/>
 </element4>

<xsl:template match="step">
 <xsl:copy>
 <xsl:copy-of select="@*" />
 <!-- Calculate the step number -->
 <xsl:attribute name="number">
  <xsl:value-of select="count( preceding-sibling::step ) + 1" />  
</xsl:attribute>  <xsl:apply-templates />  </xsl:copy> 
</xsl:template>

Can anyone help?
Thanks
________________________________
Taco Fleur - E-commerce Development Manager Shelco Searches 
& Services 
An Authorised ASIC Information Broker www.shelco.com.au 
<blocked::http://www.shelco.com.au>
Ph: + 61 7 3236 2605



--~------------------------------------------------------------------
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>
--~--





--~------------------------------------------------------------------
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>
--~--