-
Notifications
You must be signed in to change notification settings - Fork 257
/
xml-to-json.xslt
174 lines (156 loc) · 5.95 KB
/
xml-to-json.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?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:html="http://www.w3.org/1999/xhtml"
xmlns:wcag="https://www.w3.org/WAI/GL/"
exclude-result-prefixes="#all"
version="2.0">
<xsl:include href="base.xslt"/>
<xsl:output method="text"/>
<xsl:variable name="ids" select="document('ids.xml')"/>
<xsl:variable name="associations" select="document('../techniques/technique-associations.xml')"/>
<xsl:variable name="techniques" select="document('../techniques/techniques.xml')"/>
<xsl:function name="wcag:json-string" as="xs:string">
<xsl:param name="val"/>
<xsl:variable name="string">
<xsl:choose>
<xsl:when test="string($val) = $val">
<xsl:copy-of select="$val"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$val"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="replace(normalize-space($string), '"', '\\"')"/>
</xsl:function>
<xsl:template name="versions">
<xsl:choose>
<xsl:when test="@id='parsing'">["2.0", "2.1"]</xsl:when>
<xsl:when test="version = 'WCAG20'">["2.0", "2.1", "2.2"]</xsl:when>
<xsl:when test="version = 'WCAG21'">["2.1", "2.2"]</xsl:when>
<xsl:when test="version = 'WCAG22'">["2.2"]</xsl:when>
<xsl:otherwise>[]</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="altid">
<xsl:for-each select="$ids//id[. = current()/@id]/following-sibling::altid">"<xsl:value-of select="."/>"<xsl:if test="position() != last()">, </xsl:if></xsl:for-each>
</xsl:template>
<xsl:template match="/">
<xsl:message>Process sufficientNotes whatever they are</xsl:message>
{
"principles": [
<xsl:apply-templates select="//principle"/>
]
}
</xsl:template>
<xsl:template match="principle">
{
"id": "WCAG2:<xsl:value-of select="@id"/>",
"num": "<xsl:value-of select="num"/>",
"versions": <xsl:call-template name="versions"/>,
"handle": "<xsl:value-of select="normalize-space(name)"/>",
"title": "<xsl:value-of select="normalize-space(content/html:p[1])"/>",
"guidelines": [
<xsl:apply-templates select="guideline"/>
]
}<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="guideline">
{
"id": "WCAG2:<xsl:value-of select="@id"/>",
"alt_id": [<xsl:call-template name="altid"/>],
"num": "<xsl:value-of select="num"/>",
"versions": <xsl:call-template name="versions"/>,
"handle": "<xsl:value-of select="normalize-space(name)"/>",
"title": "<xsl:value-of select="normalize-space(content/html:p[1])"/>",
"successcriteria": [
<xsl:apply-templates select="success-criterion"/>
],
"techniques": [<xsl:apply-templates select="$associations//guideline[@id = current()/@id]" mode="techniques"/>]
}<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="success-criterion">
{
"id": "WCAG2:<xsl:value-of select="@id"/>",
"alt_id": [<xsl:call-template name="altid"/>],
"num": "<xsl:value-of select="num"/>",
"versions": <xsl:call-template name="versions"/>,
"level": "<xsl:value-of select="level"/>",
"handle": "<xsl:value-of select="normalize-space(name)"/>",
"title": "<xsl:value-of select="wcag:json-string(content/html:p[1])"/>",
<xsl:if test="content/html:dl">
"details": [{
"type": "ulist",
"items": [
<xsl:apply-templates select="content/html:dl/html:dt"/>
]
}],
</xsl:if>
<xsl:if test="content/html:*[@class = 'note']">
<xsl:message>Still gotta process SC notes</xsl:message>
</xsl:if>
<xsl:if test="content/html:*[not(@class = 'note')]">
<xsl:message>Still gotta process more "details" thingys</xsl:message>
</xsl:if>
"techniques": [<xsl:apply-templates select="$associations//success-criterion[@id = current()/@id]" mode="techniques"/>]
}<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="guideline | success-criterion" mode="techniques">
<xsl:apply-templates select="sufficient | advisory | failure"/>
</xsl:template>
<xsl:template match="html:dt">
{
"handle": "<xsl:value-of select="wcag:json-string(.)"/>",
"text": "<xsl:value-of select="wcag:json-string(following-sibling::html:dd[1])"/>"
}<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="sufficient | advisory | failure">
{"<xsl:value-of select="name()"/>": [
<xsl:if test="situation">
{"situations": [
<xsl:apply-templates select="situation"/>
]}
</xsl:if>
<xsl:if test="technique or ./and">
<xsl:apply-templates select="technique | ./and"/>
</xsl:if>
]}<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="situation">
{"title": "<xsl:value-of select="wcag:json-string(title)"/>",
"techniques": [
<xsl:apply-templates select="technique | ./and"/>
]}<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="technique">
{
"id":
<xsl:choose>
<xsl:when test="@id">"TECH:<xsl:value-of select="@id"/>"</xsl:when>
<xsl:otherwise>"TECH:future-<xsl:value-of select="ancestor::element()[name() = 'success-criterion' or name() = 'guideline'][1]/@id"/>-<xsl:value-of select="count(preceding::technique[ancestor::element()/@id = current()/@id]) + 1"/>"</xsl:otherwise>
</xsl:choose>
,
"title":
<xsl:choose>
<xsl:when test="@id">"<xsl:value-of select="wcag:json-string($techniques//technique[@id = current()/@id]/title)"/>"</xsl:when>
<xsl:otherwise>"<xsl:value-of select="wcag:json-string(title)"/>"</xsl:otherwise>
</xsl:choose>
<xsl:if test="using">
,<xsl:apply-templates select="using"/>
</xsl:if>
}
<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="and">
{"and": [
<xsl:apply-templates select="technique"/>
]}
<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
<xsl:template match="using">
"using": [
<xsl:apply-templates select="technique | ./and"/>
]
</xsl:template>
</xsl:stylesheet>