tags:

views:

10

answers:

1

I query and sort alphabetically a bunch of XML elements, to which I apply a template, to produce an alphabetical list. I'd like to prefix the nodes with the same initial by this very initial:

A
Abe
Amel
Andrew
B
Bobby
Benny
...

The preceding-sibling axis is relative to the document, not the nodeset. What can I do?

A: 

What you've described here is a grouping problem. The standard way to handle grouping in xslt 1 is the "Muenchian" method. There's a very detailed explanation about it here: http://www.jenitennison.com/xslt/grouping/muenchian.html

The basic idea is that you create a key which specifies what you want to group by. In this case, you'd create a key matching the person's node, using the first letter of their name.

Then you write a loop which loops through the people, and checks if each one is the first one to match that given key (first letter). If it is, then you put in one of your grouping dividers.

Then you have a nested loop (or apply-templates) which has a match rule that only picks up nodes with that first letter. You can sort them using xslt:sort, and output them.

If you can post a sample of your actual XML (rather than just your desired output), then I can write you an example stylesheet to parse it.

Erica