Welcome to WebmasterWorld Guest from 54.167.144.170

Forum Moderators: httpwebwitch

Count how many "patterns" before current node

   
1:49 pm on Oct 27, 2008 (gmt 0)

5+ Year Member



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>

7:59 pm on Oct 27, 2008 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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?

5:31 pm on Oct 28, 2008 (gmt 0)

5+ Year Member



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 left-side 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 3-4 levels, so if Saxon could do it for me I would be glad.

Thankful for any input
/Claes

7:09 pm on Oct 28, 2008 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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
. 1-1
. 1-2
. 1-3
2
. 2-1
. . 2-1-1
. . 2-1-2
. 2-2

c what i mean

6:53 am on Oct 29, 2008 (gmt 0)

5+ Year Member



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

1:36 pm on Oct 29, 2008 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



'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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month