Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The next stage is to convert this flat sequence into a hierarchy, in which lines with
level=“1”
(for example) turn into XML elements that contain the corresponding
level=“2”
lines.
Any problem that involves adding hierarchic levels to the result tree can be regarded as a grouping problem, and it should therefore be no surprise that we tackle it using the
group-starting-with
attribute, whose value is a match pattern that is used to recognize the first element in each group.
A single application of
group-starting-with=“*[xs:integer(@level) eq $level]”>
select=“current-group()[position() != 1]”/>
select=“$level + 1”/>
In the recursive call I originally set the population parameter to
current-group()
except .
. This ought to work, but it produces incorrect output in Altova. Altova also fails to indent the output—this is reasonable, since the specification advises against indenting data that is known to contain mixed content.
When this is called to process all the
$level
parameter set to zero, it forms one group for each line having the attribute
level=“0”
, containing that line and all the following lines up to the next one with
level=“0”
. It then processes each of these groups by creating an element to represent the level 0 line (the name of this element is taken from the GEDCOM tag, and its ID and REF attributes are copied unless they are empty), and constructs the content of this new element by means of a recursive call, processing all elements in the group except the first, and looking this time for level 1 lines as the ones that start a new group. The process continues until there are no lines at the next level (the
The remaining code in the stylesheet simply invokes this recursive template to process all the lines at level 0:
This main template represents the entry point to the stylesheet. I added the attribute
match=“/”
because at the time of writing, the Altova XSLT 2.0 processor requires a source document to be supplied, even though it is not used. In principle, however, XSLT 2.0 allows a transformation to be invoked with no source document, by naming a template where execution is to start. I use the name
main
as a matter of convention.