[xsl] Count of previous nodes until a child node of a previous node is found with certain attribute value

Subject: [xsl] Count of previous nodes until a child node of a previous node is found with certain attribute value
From: "Jeremy Freedman" <jeremy.freedman@xxxxxxxxx>
Date: Sat, 26 May 2007 15:21:33 -0400
Hi all,

I'm the first to admit I'm not XSL expert, but I've been stuck on this
one for hours and I'm hoping someone can help me out.

I've found similar posts where a user is looking for a count of
following nodes until an attribute is found, but not close enough to
what I'm looking to do.

Assuming we have this XML structure:

<body>
   <p>
         text
   </p>
  <p>
         text2
   </p>
   <p>
         <component name="newpage" />
   </p>
   <p>
         text3
   </p>
  <p>
         text4
   </p>
   <p>
         text5
   </p>
  <p>
         text6
   </p>
   <p>
         <component name="newpage" />
   </p>
   <p>
         text7
   </p>
  <p>
         text8
   </p>
   <p>
         text9
   </p>
</body>


I want to be able to find the count of preceding "p" nodes from the p node I am on, up until I find a "p" node with a child node named "component" with attribute name=newpage.

Examples:

If I am on the p node whose value is "text8", I should get a count of
1 because there is only 1 previous p node before I find a p node with
the component node with attribute name=newpage.

p node value            Count
text                          0
text2                        1
text3                        0
text4                        1
text5                        2
text6                        3
text7                        0
text8                        1
text9                        2


You will be my personal hero is you can help me out!


Much thanks,

Jeremy

Current Thread