forked from Klortho/DtdAnalyzer
- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscaffold.xsl
executable file
·153 lines (139 loc) · 7.11 KB
/
scaffold.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
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
<!-- ************************************************************************************************ -->
<!-- Stylesheet will convert DtdAnalyzer output into a scaffolded XSLT script that contains a template
for every element in the original DTD. These templates can be changed to convert content to
a new schema. Each template header contains the content model of the element to aid in analysys.
Note that if the DTD declares xmlns namespace declarations as attributes, we must redeclare
the same namespace and prefix bindings on the root element. We assume that the namespace URIs are
probably declared as the default value of the pseudo-attribute. If no default value is found,
we output a fake URI. You will need to manually change these values to the correct namespace
URI.
Author: Demian Hess, Avalon Consulting, LLC
Date: August 30, 2012 -->
<!-- ************************************************************************************************ -->
<xsl:stylesheetversion="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="urn:xslt:alias">
<xsl:outputmethod="xml"indent="yes"/>
<xsl:namespace-aliasstylesheet-prefix="x"result-prefix="xsl"/>
<xsl:strip-spaceelements="*"/>
<xsl:variablename="crlf"select="' '" />
<!-- ************************************************************************************************ -->
<!-- Template for: / -->
<!-- ================================================================================================ -->
<!-- Create stylesheet -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<xsl:templatematch="/">
<x:stylesheetversion="2.0">
<!-- Search for attributes that are actually namespace declarations and add to document element -->
<xsl:for-eachselect="/declarations/attributes/attribute[starts-with(@name, 'xmlns:')]">
<xsl:choose>
<xsl:whentest="attributeDeclaration/@defaultValue">
<xsl:namespacename="{substring-after(@name, ':')}"select="attributeDeclaration[@defaultValue][1]/@defaultValue" />
</xsl:when>
<xsl:otherwise>
<xsl:namespacename="{substring-after(@name, ':')}"select="'urn:unknown:namespace:uri'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<x:outputmethod="xml"indent="yes"/>
<xsl:apply-templatesselect="/declarations/elements/element">
<xsl:sortselect="@name"/>
</xsl:apply-templates>
</x:stylesheet>
</xsl:template>
<!-- ************************************************************************************************ -->
<!-- Template for: element -->
<!-- ================================================================================================ -->
<!-- Create template for each element -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<xsl:templatematch="element">
<xsl:variablename="curr-element"select="@name"/>
<xsl:variablename="attributes"select="/declarations/attributes/attribute/attributeDeclaration[@element eq $curr-element]"/>
<xsl:value-ofselect="$crlf"/>
<xsl:comment> ************************************************************************************************ </xsl:comment>
<xsl:value-ofselect="$crlf"/>
<xsl:comment> Template for: <xsl:value-ofselect="@name"/> <xsl:text> </xsl:text></xsl:comment>
<xsl:value-ofselect="$crlf"/>
<xsl:comment> ================================================================================================ </xsl:comment>
<xsl:value-ofselect="$crlf"/>
<xsl:comment>
<xsl:text> Content model for element:</xsl:text>
<xsl:value-ofselect="$crlf"/>
<xsl:call-templatename="line-wrap">
<xsl:with-paramname="str"select="content-model/@spaced"/>
</xsl:call-template>
<xsl:iftest="$attributes">
<xsl:value-ofselect="$crlf"/>
<xsl:value-ofselect="$crlf"/>
<xsl:text>Attributes:</xsl:text>
<xsl:value-ofselect="$crlf"/>
</xsl:if>
<xsl:for-eachselect="$attributes">
<xsl:sortselect="parent::attribute/@name"/>
<xsl:text> -</xsl:text>
<xsl:value-ofselect="parent::attribute/@name"/>
<xsl:iftest="@type | @mode | @defaultValue">
<xsl:text> (</xsl:text>
<xsl:for-eachselect="@type | @mode | @defaultValue">
<xsl:choose>
<xsl:whentest="local-name() eq 'type'">
<xsl:text>Type: </xsl:text>
<xsl:value-ofselect="."/>
</xsl:when>
<xsl:whentest="local-name() eq 'mode'">
<xsl:text>Mode: </xsl:text>
<xsl:value-ofselect="."/>
</xsl:when>
<xsl:whentest="local-name() eq 'defaultValue'">
<xsl:text>Default Value: </xsl:text>
<xsl:value-ofselect="."/>
</xsl:when>
</xsl:choose>
<xsl:iftest="position() ne last()">; </xsl:if>
</xsl:for-each>
<xsl:text>)</xsl:text>
<xsl:value-ofselect="$crlf"/>
</xsl:if>
</xsl:for-each>
</xsl:comment>
<xsl:value-ofselect="$crlf"/>
<xsl:comment> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </xsl:comment>
<xsl:value-ofselect="$crlf"/>
<x:templatematch="{@name}">
<x:copy>
<xsl:iftest="$attributes">
<x:copy-ofselect="@*"/>
</xsl:if>
<x:apply-templates/>
</x:copy>
</x:template>
<xsl:value-ofselect="$crlf"/>
</xsl:template>
<!-- ************************************************************************************************ -->
<!-- Named template: line-wrap -->
<!-- ================================================================================================ -->
<!-- Wrap lines every 80 chars (approximately) -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<xsl:templatename="line-wrap">
<xsl:paramname="str"select="''"/>
<xsl:paramname="counter"select="1"/>
<xsl:variablename="first-char"select="substring($str, 1, 1)"/>
<xsl:choose>
<xsl:whentest="string-length($str) eq 0"/>
<xsl:whentest="$counter ge 80 and $first-char eq ' '">
<xsl:value-ofselect="$crlf"/>
<xsl:call-templatename="line-wrap">
<xsl:with-paramname="str"select="substring($str, 2)"/>
<xsl:with-paramname="counter"select="1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-ofselect="$first-char"/>
<xsl:call-templatename="line-wrap">
<xsl:with-paramname="str"select="substring($str, 2)"/>
<xsl:with-paramname="counter"select="$counter + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>