-
Notifications
You must be signed in to change notification settings - Fork 10
/
tocToModules.xslt
196 lines (196 loc) · 9.75 KB
/
tocToModules.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<!--
- noEx = no example tab
- noExt = no extensions tab
- noMap = no mapping tab
- noProfile = no profile tab
- isDoc = part of documents hierarchy
- isDt = part of datatype hierarchy
- isFt = part of foundation types hierarchy
- custom = custom page created for this, so not using generic resource/data type tab layout
-->
<xsl:variable name="datatypes" as="element(datatypes)">
<datatypes>
<datatype page="resource" name="Base Resource Definitions" noEx="Y" noMap="Y" custom="Y"/>
<datatype page="DomainResource" name="DomainResource Resource" noEx="Y" noMap="Y"/>
<datatype page="CanonicalResource" name="Resource CanonicalResource - Content" noEx="Y" noMap="Y"/>
<datatype page="MetadataResource" name="Resource MetadataResource - Content" noEx="Y" noMap="Y"/>
<datatype page="Dosage" name="Dosage" tab="Dosage Detail" isDt="Y"/>
<datatype page="Extension" name="Extensibility" tab="Extensibility" isDt="Y" noEx="Y"/>
<datatype page="Narrative" name="Narrative" tab="Narrative" noMap="Y" noExt="Y" noProfile="Y" isDt="Y"/>
<datatype page="ElementDefinition" name="Element Definition" tab="ElementDefinition Detail" isDt="Y"/>
<datatype page="MarketingStatus" name="MarketingStatus" tab="MarketingStatus Detail" isDt="Y"/>
<datatype page="ProductShelfLife" name="ProductShelfLife" tab="ProductShelfLife Detail" isDt="Y"/>
<datatype page="datatypes" name="Datatypes" tab="Datatypes" isDt="Y" custom="Y"/>
<datatype page="types" name="FHIR Type Framework" tab="Type Framework" isDt="Y" noEx="Y" custom="Y"/>
<datatype page="metadatatypes" name="MetaDatatypes" tab="Meta Datatypes" isDt="Y" custom="Y"/>
<datatype page="references" name="Resource References" tab="References" noEx="Y" noMap="Y" isDt="Y" custom="Y"/>
</datatypes>
</xsl:variable>
<xsl:variable name="special" as="element(special)">
<special>
<resource page="Resource" name="Base Resource Definitions"/>
<resource page="DomainResource" name="DomainResource Resource"/>
</special>
</xsl:variable>
<xsl:variable name="phase1" as="element(pages)">
<xsl:apply-templates mode="phase1" select="/table"/>
</xsl:variable>
<xsl:variable name="phase2" as="element(pages)">
<pages>
<xsl:apply-templates mode="phase2" select="$phase1/page[1]"/>
</pages>
</xsl:variable>
<xsl:variable name="phase3" as="element(modules)">
<modules>
<xsl:for-each select="$phase2/page">
<module name="{if (ends-with(@title, 'Module')) then substring-before(@title, ' Module') else @title}">
<xsl:apply-templates mode="phase3" select="page"/>
</module>
</xsl:for-each>
</modules>
</xsl:variable>
<xsl:template match="/">
<xsl:text>{"resources": {</xsl:text>
<xsl:variable name="foundationSection" select="substring-before($phase3/module[@name='Foundation']/resource[last()]/@section, '.')"/>
<xsl:variable name="foundationSubSection" select="number(substring-before(substring-after($phase3/module[@name='Foundation']/resource[last()]/@section, '.'), '.'))"/>
<xsl:value-of select="concat('"extension-registry":{"module":"Foundation","section":"', $foundationSection, '.', $foundationSubSection+1,
'"},"conversion-registry":{"module":"Foundation","section":"', $foundationSection, '.', $foundationSubSection+2,
'"},"search-params":{"module":"Foundation","section":"', $foundationSection, '.', $foundationSubSection+3,
'"},')"/>
<xsl:for-each select="$phase3/module/resource">
<xsl:value-of select="concat('"', @title, '":{"module":"', parent::module/@name, '","section":"')"/>
<xsl:choose>
<xsl:when test="@inDt='Y'">
<xsl:variable name="precedingDt" select="count(preceding-sibling::*[@inDt='Y'])"/>
<xsl:for-each select="parent::module/resource[@title='Datatypes']">
<xsl:variable name="sectionParts" select="tokenize(@section2, '\.')"/>
<xsl:variable name="sectionBase" select="string-join($sectionParts[position()!=last()], '.')"/>
<xsl:variable name="sectionLast" select="$sectionParts[position()=last()]"/>
<xsl:variable name="section" select="concat($sectionBase, '.', number($sectionLast) + $precedingDt + 1)"/>
<xsl:value-of select="concat($section, '.1","section2":"', $section, '.2')"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(@section, '","section2":"', @section2)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="@noEx='Y'">
<xsl:text>","noEx":"Y</xsl:text>
</xsl:if>
<xsl:if test="@noExt='Y'">
<xsl:text>","noExt":"Y</xsl:text>
</xsl:if>
<xsl:if test="@noMap='Y'">
<xsl:text>","noMap":"Y</xsl:text>
</xsl:if>
<xsl:if test="@noProfile='Y'">
<xsl:text>","noProfile":"Y</xsl:text>
</xsl:if>
<xsl:if test="@custom='Y'">
<xsl:text>","custom":"Y</xsl:text>
</xsl:if>
<xsl:if test="@inDoc='Y'">
<xsl:text>","inDoc":"Y</xsl:text>
</xsl:if>
<xsl:if test="@inDt='Y'">
<xsl:text>","inDt":"Y</xsl:text>
</xsl:if>
<xsl:if test="@inFt='Y'">
<xsl:text>","inFt":"Y</xsl:text>
</xsl:if>
<xsl:if test="@isDt='Y'">
<xsl:text>","isDt":"Y</xsl:text>
</xsl:if>
<xsl:if test="@tab">
<xsl:text>","tab":"</xsl:text>
<xsl:value-of select="@tab"/>
</xsl:if>
<xsl:text>"}</xsl:text>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
<xsl:text>}}</xsl:text>
</xsl:template>
<xsl:template mode="phase1" match="table">
<pages>
<xsl:for-each select="tr/td/a[not(@style) and not(starts-with(@title, 'CodeSystem') or starts-with(@title, 'ValueSet'))]">
<page num="{substring-before(., ' ')}" title="{substring-after(., ' ')}"/>
</xsl:for-each>
</pages>
</xsl:template>
<xsl:template mode="phase2" match="page">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:if test="exists(following-sibling::page[1][starts-with(@num, current()/@num)])">
<xsl:variable name="matching" as="element(page)">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="match" select="following-sibling::page[1]">
<xsl:with-param name="match" select="@num"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:variable>
<xsl:apply-templates mode="phase2" select="$matching/page[1]"/>
</xsl:if>
</xsl:copy>
<xsl:apply-templates mode="phase2" select="following-sibling::page[not(starts-with(@num, current()/@num))][1]"/>
</xsl:template>
<xsl:template mode="match" match="page">
<xsl:param name="match"/>
<xsl:if test="starts-with(@num, $match)">
<xsl:copy-of select="."/>
<xsl:apply-templates mode="match" select="following-sibling::page[1]">
<xsl:with-param name="match" select="$match"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
<xsl:template mode="phase3" match="page">
<xsl:param name="inDoc" select="false()"/>
<xsl:param name="inDt" select="false()"/>
<xsl:param name="inFt" select="false()"/>
<xsl:if test="@title='Documentation Index'">
<xsl:apply-templates mode="phase3" select="page">
<xsl:with-param name="inDoc" select="true()"/>
</xsl:apply-templates>
</xsl:if>
<xsl:if test="@title='Datatypes'">
<xsl:apply-templates mode="phase3" select="page">
<xsl:with-param name="inDoc" select="$inDoc"/>
<xsl:with-param name="inDt" select="true()"/>
</xsl:apply-templates>
</xsl:if>
<xsl:if test="@title='FHIR Type Framework'">
<xsl:apply-templates mode="phase3" select="page">
<xsl:with-param name="inDoc" select="$inDoc"/>
<xsl:with-param name="inFt" select="true()"/>
</xsl:apply-templates>
</xsl:if>
<xsl:if test="(starts-with(@title, 'Resource ') and ends-with(@title, ' - Content')) or exists($datatypes/datatype[@name=current()/@title])">
<xsl:variable name="num" select="if (page) then page[last()]/@num else concat(@num, '.12')"/>
<xsl:variable name="nums" select="tokenize($num, '\.')"/>
<xsl:variable name="prefix" select="string-join($nums[position()<last()], '.')"/>
<xsl:variable name="newNum" select="concat($prefix, '.', number($nums[last()])+1)"/>
<xsl:variable name="newNum2" select="concat($prefix, '.', number($nums[last()])+2)"/>
<resource title="{substring-before(substring-after(@title, 'Resource '), ' - Content')}">
<!-- If we're already 4 deep in the hierarchy (ToC doesn't go deeper), then presume the last existing section is 12 - which is currently true for the type-framework resources -->
<xsl:attribute name="section" select="$newNum"/>
<xsl:attribute name="section2" select="$newNum2"/>
<xsl:for-each select="$datatypes/datatype[@name=current()/@title]">
<xsl:attribute name="title" select="@page"/>
<xsl:copy-of select="@*[not(local-name(.)=('name', 'page'))]"/>
</xsl:for-each>
<xsl:if test="$inDoc">
<xsl:attribute name="inDoc" select="'Y'"/>
</xsl:if>
<xsl:if test="$inDt">
<xsl:attribute name="inDt" select="'Y'"/>
</xsl:if>
<xsl:if test="$inFt">
<xsl:attribute name="inFt" select="'Y'"/>
</xsl:if>
</resource>
</xsl:if>
</xsl:template>
</xsl:stylesheet>