1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<!-- generate a key for eachnode -->
<xsl:key name="childNode" match="/root/row" use="parentid"/>
<!-- selects the root nodes from hierarchies-->
<xsl:template match="/root">
<hierarchies>
<xsl:apply-templates select="/root/row[name='L1-A']"/>
</hierarchies>
</xsl:template>
<!-- apply templates for root nodes-->
<xsl:template match="/root/row[name='L1-A']">
<hierarchy>
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<child>
<xsl:apply-templates select="key('childNode',./id)"/>
</child>
</xsl:copy>
</hierarchy>
</xsl:template>
<!-- apply templates for child nodes-->
<xsl:template match="root/row">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<child>
<xsl:apply-templates select="key('childNode',./id)"/>
</child>
</xsl:copy>
</xsl:template>
<!-- copy content -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
|