forked from DFHack/df-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lower-2.xslt
79 lines (71 loc) · 2.78 KB
/
lower-2.xslt
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Second pass of lowering:
- Detect incorrectly placed fields
- Fold container item fields into a single ld:item subelement.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ld="http://github.com/peterix/dfhack/lowered-data-definition">
<!-- Generic walk -->
<xsl:template match="@*">
<xsl:copy-of select='.'/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select='@*|node()'/>
</xsl:copy>
</xsl:template>
<!-- Detect invalid fields & methods -->
<xsl:template match="ld:field[not(@ld:level) or (@ld:level < 0)]" priority='10'>
<xsl:message terminate='yes'>
Unexpected field: <xsl:copy-of select='.'/>
</xsl:message>
</xsl:template>
<xsl:template match="ld:vmethod[not(@ld:level) or not(@ld:level = 1)]" priority='10'>
<xsl:message terminate='yes'>
Unexpected method: <xsl:copy-of select='.'/>
</xsl:message>
</xsl:template>
<!-- Unify containers -->
<xsl:template match="ld:field" priority='8'>
<xsl:param name="tag" select="name(.)"/>
<xsl:element name='{$tag}'>
<xsl:apply-templates select='@*|node()'/>
</xsl:element>
</xsl:template>
<xsl:template match="*[@ld:is-container]" priority='9'>
<xsl:param name="tag" select="name(.)"/>
<xsl:element name='{$tag}'>
<xsl:apply-templates select='@*'/>
<xsl:choose>
<xsl:when test='count(ld:field) <= 1'>
<xsl:apply-templates select='node()'>
<xsl:with-param name='tag' select="'ld:item'"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<!-- This destroys formatting, but it seems inevitable. -->
<xsl:copy-of select='text()[1]'/>
<ld:item ld:meta='compound'>
<xsl:attribute name='ld:level'><xsl:value-of select='@ld:level'/></xsl:attribute>
<xsl:apply-templates select='@key-field'/>
<xsl:apply-templates select='ld:field|text()'/>
</ld:item>
<xsl:apply-templates select='node()[not(self::ld:field)]'/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match='ret-type[count(ld:field)=1]'>
<xsl:apply-templates select='ld:field'>
<xsl:with-param name='tag' select="'ret-type'"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
<!--
Local Variables:
indent-tabs-mode: nil
nxml-child-indent: 4
End:
-->