 Count how many "patterns" before current node

Claes100
 Msg#: 3774203 posted 1:49 pm on Oct 27, 2008 (gmt 0)  Hi, I am stuck with my XSLT... In each <elem> node below I need to count how many times the pattern or sequence "<node>/<node> (but only the first <node> sibling!)" has occured before the current <elem> node. Sort of a submenu index. Something like count(ancestor::node/node[position()='0']) This is how my tree look like and the desired count. Any help much appreciated! Thanks! (Sorry for the indentation, apparently spaces didn't work) <root> <node><elem>...</elem></node> count = 0 <node><elem>...</elem></node> count = 0 <node><elem>...</elem> count = 0 <node><elem>...</elem></node> count = 1 <node><elem>...</elem></node> count = 1 <node><elem>...</elem> count = 1 <node><elem>...</elem></node> count = 2 <node><elem>...</elem></node> count = 2 </node> </node> <node><elem>...</elem> count = 0 <node><elem>...</elem></node> count = 3 <node><elem>...</elem></node> count = 3 </node> <node><elem>...</elem></node> count = 0 <node><elem>...</elem> count = 0 <node><elem>...</elem></node> count = 4 </node </root>

httpwebwitch
 Msg#: 3774203 posted 7:59 pm on Oct 27, 2008 (gmt 0)  to preserve indentation, use "pre" formatting  see the "Styling Codes" link beside the text box for more BBcode hints I don't quite understand the pattern. You're not just counting the depth of the current node (ie the length of its ancestry)... you're also incrementing it like an integer, similar to an ordered list? I don't understand why Line 7 counts "1", while Line 12 counts "0", and what is the difference between the elems that count 3 and 4 [pre] <root> . <node><elem>...</elem></node> count = 0 . <node><elem>...</elem></node> count = 0 . <node><elem>...</elem> count = 0 . . <node><elem>...</elem></node> count = 1 . . <node><elem>...</elem></node> count = 1 . . <node><elem>...</elem> count = 1 . . . <node><elem>...</elem></node> count = 2 . . . <node><elem>...</elem></node> count = 2 . . </node> . </node> . <node><elem>...</elem> count = 0 . . <node><elem>...</elem></node> count = 3 . . <node><elem>...</elem></node> count = 3 . </node> . <node><elem>...</elem></node> count = 0 . <node><elem>...</elem> count = 0 . . <node><elem>...</elem></node> count = 4 . </node> </root> [/pre]
maybe this could be done with a recursive template, using parameters like a scoped variable. What's the application? Are you rendering a hierarchy tree view using indentation, or something like that?

Claes100
 Msg#: 3774203 posted 5:31 pm on Oct 28, 2008 (gmt 0)  Hi, I am generating an HTML site with a javascript tree menu navigation. The <node> elements' purpose is only to get the hierarchy in the tree menu. Right now I am investigating a new, similar javascript that has a JS API, with which I can dynamically highligt the "menu node" and expand parent nodes in the tree menu when the page loads. One of the JS functions requires "subMenuIndex" which I named "count" in my original post. Maybe itīs clearer with a simplified (but perhaps stupid :D) example, see it as a leftside expandable tree menu:
<root> . <node>Map of the earth</node> . . . subMenuIndex= 0 (no parent node) . <node>Map of the moon </node> . . . subMenuIndex= 0 (no parent node) . <node>Countries in Europe . . . . . subMenuIndex= 0 (no parent node) . . <node>Norway</node> . . . . . . . subMenuIndex= 1 (1st sub in tree) . . <node>Sweden</node> . . . . . . . subMenuIndex= 1 (1st sub in tree) . . <node>Cities in France . . . . . .subMenuIndex= 1 (1st sub in tree) . . . <node>Paris</node> . . . . . . .subMenuIndex= 2 (2nd sub in tree) . . . <node>Lyon </node> . . . . . . .subMenuIndex= 2 (2nd sub in tree) . . </node> . </node> . <node>Countries in South America . .subMenuIndex= 0 (no parent node) . . <node>Brazil</node> . . . . . . . subMenuIndex= 3 (3rd sub in tree) . . <node>Peru </node> . . . . . . . .subMenuIndex= 3 (3rd sub in tree) . </node> . <node>Map of South America</node> . subMenuIndex= 0 (no parent node) . <node>Countries in North America . .subMenuIndex= 0 (no parent node) . . <node>Canada</node> . . . . . . . subMenuIndex= 4 (4th sub in tree) </node> </root> One solution of course is to have the subMenuIndex as an attribute to each node, but in our generated site thereīs like 60 nodes in 34 levels, so if Saxon could do it for me I would be glad. Thankful for any input /Claes

httpwebwitch
 Msg#: 3774203 posted 7:09 pm on Oct 28, 2008 (gmt 0)  oh! I get it! those are like IDs for the parent node. (hand meets forehead) Does it matter if the integers are sequential? and... do they need to be integers, or could it be a string, like an element ID? a recursive XSLT solution will be easier if the end result could be more like this 1 . 11 . 12 . 13 2 . 21 . . 211 . . 212 . 22 c what i mean

Claes100
 Msg#: 3774203 posted 6:53 am on Oct 29, 2008 (gmt 0)  No, they don't have to be sequential, nor integers. Can you give me a clue of a recursive solution, haven't done that. Yet... Thanks again! /C

httpwebwitch
 Msg#: 3774203 posted 1:36 pm on Oct 29, 2008 (gmt 0)  'fraid I won't have time to work on any XSLT today, but if there's no other response by tomorrow I'll see what I can whip up by way of an example. In the mean time, go do some research to learn about using recursive templates. Basically it's a template applied to a node, that loops through its children and for each child it calls itself. So when it's run on a root node it effectively traverses the whole tree. When you start working with hierarchies and trees, recursive logic is a really important notion, you'll use it a lot. ttyl hww


