-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml2yml.xsl
121 lines (116 loc) · 5.01 KB
/
xml2yml.xsl
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:appconf="http://www.bbaw.de/telota/software/ediarum/web/appconf"
xmlns:yml="http://www.bbaw.de/telota/software/ediarum/web/yml"
exclude-result-prefixes="xs appconf yml"
version="1.0">
<xsl:output method="text"/>
<xsl:template match="/yml:yml" priority="1">
<xsl:for-each select="*">
<xsl:call-template name="element">
<xsl:with-param name="element" select="."/>
<xsl:with-param name="indent" select="0"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="element">
<xsl:param name="element"/>
<xsl:param name="indent"/>
<xsl:variable name="loop-1">
<xsl:call-template name="loop">
<xsl:with-param name="i" select="1"/>
<xsl:with-param name="to" select="$indent"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="loop-0">
<xsl:call-template name="loop">
<xsl:with-param name="i" select="0"/>
<xsl:with-param name="to" select="$indent"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<!-- Array -->
<xsl:when test="$element/self::yml:item[not(child::*)]">
<xsl:text>- </xsl:text>
<xsl:value-of select="$element"/>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:when test="$element/self::yml:item">
<xsl:for-each select="$element/*[1]">
<xsl:text>- </xsl:text>
<xsl:call-template name="element">
<xsl:with-param name="element" select="."/>
<xsl:with-param name="indent" select="$indent +1"/>
</xsl:call-template>
</xsl:for-each>
<xsl:for-each select="$element/*[position()>1]">
<xsl:value-of select="$loop-1"/>
<xsl:text> </xsl:text>
<xsl:call-template name="element">
<xsl:with-param name="element" select="."/>
<xsl:with-param name="indent" select="$indent +1"/>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<!-- Object -->
<xsl:when test="$element/self::yml:object">
<xsl:value-of select="$element/@name"/>
<xsl:text>:
</xsl:text>
<xsl:for-each select="$element/*">
<xsl:value-of select="$loop-0"/>
<xsl:call-template name="element">
<xsl:with-param name="element" select="."/>
<xsl:with-param name="indent" select="$indent +1"/>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<!-- Object -->
<xsl:when test="$element/child::*">
<xsl:value-of select="name($element)"/>
<xsl:text>:
</xsl:text>
<xsl:for-each select="$element/*">
<xsl:value-of select="$loop-0"/>
<xsl:call-template name="element">
<xsl:with-param name="element" select="."/>
<xsl:with-param name="indent" select="$indent +1"/>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<!-- String -->
<xsl:when test="$element/self::yml:string">
<xsl:value-of select="$element/@name"/>
<xsl:text>: '</xsl:text>
<xsl:value-of select="string($element)"/>
<xsl:text>'
</xsl:text>
</xsl:when>
<!-- Plain yml -->
<xsl:when test="$element/self::yml:plain"><xsl:value-of select="$element/text()"/>
<xsl:text>
</xsl:text>
</xsl:when>
<!-- Key-Value -->
<xsl:when test="not($element/child::*)">
<xsl:value-of select="name($element)"/>
<xsl:text>: </xsl:text>
<xsl:choose>
<xsl:when test="string($element) =''">''</xsl:when>
<xsl:otherwise>
<xsl:value-of select="string($element)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="i"/>
<xsl:param name="to"/>
<xsl:text> </xsl:text>
<xsl:if test="$i < $to">
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i +1"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>