Forum Moderators: open
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>
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?
<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
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
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