| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
|---|
| 2 |
|
|---|
| 3 |
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp " ">]> |
|---|
| 4 |
|
|---|
| 5 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
|---|
| 6 |
<!-- |
|---|
| 7 |
XSL Stylesheet to normalize a database schema |
|---|
| 8 |
--> |
|---|
| 9 |
|
|---|
| 10 |
<!-- |
|---|
| 11 |
Output indented UTF 8 XML |
|---|
| 12 |
--> |
|---|
| 13 |
<xsl:output method="xml" indent="yes" encoding="UTF-8" /> |
|---|
| 14 |
|
|---|
| 15 |
<!-- |
|---|
| 16 |
Matches root database node, the only allowed root node |
|---|
| 17 |
|
|---|
| 18 |
Starts the normalization process |
|---|
| 19 |
--> |
|---|
| 20 |
<xsl:template match='/database'> |
|---|
| 21 |
<database> |
|---|
| 22 |
<xsl:if test='not(boolean(@defaultIdMethod))'> |
|---|
| 23 |
<xsl:attribute name='defaultIdMethod'>native</xsl:attribute> |
|---|
| 24 |
</xsl:if> |
|---|
| 25 |
<xsl:if test='not(boolean(@defaultPhpNamingMethod))'> |
|---|
| 26 |
<xsl:attribute name='defaultPhpNamingMethod'>underscore</xsl:attribute> |
|---|
| 27 |
</xsl:if> |
|---|
| 28 |
<xsl:if test='not(boolean(@heavyIndexing))'> |
|---|
| 29 |
<xsl:attribute name='heavyIndexing'>false</xsl:attribute> |
|---|
| 30 |
</xsl:if> |
|---|
| 31 |
<xsl:apply-templates select='@*'/> |
|---|
| 32 |
<xsl:apply-templates select='external-schema'/> |
|---|
| 33 |
<xsl:apply-templates select='table'/> |
|---|
| 34 |
</database> |
|---|
| 35 |
</xsl:template> |
|---|
| 36 |
|
|---|
| 37 |
<!-- |
|---|
| 38 |
Normalizes any defaultPhpNamingMethod attribute by making it lowercase |
|---|
| 39 |
--> |
|---|
| 40 |
<xsl:template match='@defaultPhPNamingMethod'> |
|---|
| 41 |
<xsl:attribute name='defaultPhPNamingMethod'><xsl:value-of select='translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")'/></xsl:attribute> |
|---|
| 42 |
</xsl:template> |
|---|
| 43 |
|
|---|
| 44 |
<!-- |
|---|
| 45 |
Normalizes any onDelete attribute by making it lowercase, or none if it is empty (makes onDelete='' act the same as onDelete='none') |
|---|
| 46 |
--> |
|---|
| 47 |
<xsl:template match='@onDelete' name='onDelete'> |
|---|
| 48 |
<xsl:choose> |
|---|
| 49 |
<xsl:when test='.=""'> |
|---|
| 50 |
<xsl:attribute name='onDelete'>none</xsl:attribute> |
|---|
| 51 |
</xsl:when> |
|---|
| 52 |
<xsl:otherwise> |
|---|
| 53 |
<xsl:attribute name='onDelete'><xsl:value-of select='translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")'/></xsl:attribute> |
|---|
| 54 |
</xsl:otherwise> |
|---|
| 55 |
</xsl:choose> |
|---|
| 56 |
</xsl:template> |
|---|
| 57 |
|
|---|
| 58 |
<!-- |
|---|
| 59 |
Handle OnDelete the same as onDelete |
|---|
| 60 |
--> |
|---|
| 61 |
<xsl:template match='@OnDelete'> |
|---|
| 62 |
<xsl:call-template name='onDelete'/> |
|---|
| 63 |
</xsl:template> |
|---|
| 64 |
|
|---|
| 65 |
<!-- |
|---|
| 66 |
Normalizes any onUpdate attribute by making it lowercase, or none if it is empty (similar to onDelete) |
|---|
| 67 |
--> |
|---|
| 68 |
<xsl:template match='@onUpdate' name='onUpdate'> |
|---|
| 69 |
<xsl:choose> |
|---|
| 70 |
<xsl:when test='.=""'> |
|---|
| 71 |
<xsl:attribute name='onUpdate'>none</xsl:attribute> |
|---|
| 72 |
</xsl:when> |
|---|
| 73 |
<xsl:otherwise> |
|---|
| 74 |
<xsl:attribute name='onUpdate'><xsl:value-of select='translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")'/></xsl:attribute> |
|---|
| 75 |
</xsl:otherwise> |
|---|
| 76 |
</xsl:choose> |
|---|
| 77 |
</xsl:template> |
|---|
| 78 |
|
|---|
| 79 |
<!-- |
|---|
| 80 |
Handle OnUpdate the same as onUpdate |
|---|
| 81 |
--> |
|---|
| 82 |
<xsl:template match='@OnUpdate'> |
|---|
| 83 |
<xsl:call-template name='onUpdate'/> |
|---|
| 84 |
</xsl:template> |
|---|
| 85 |
|
|---|
| 86 |
<!-- |
|---|
| 87 |
Tranlate IdMethod attribute to idMethod attribute |
|---|
| 88 |
--> |
|---|
| 89 |
<xsl:template match='@IdMethod'> |
|---|
| 90 |
<xsl:attribute name='idMethod'><xsl:value-of select='.'/></xsl:attribute> |
|---|
| 91 |
</xsl:template> |
|---|
| 92 |
|
|---|
| 93 |
<!-- |
|---|
| 94 |
Just copy any attribute |
|---|
| 95 |
--> |
|---|
| 96 |
<xsl:template match='@*' priority='-1'> |
|---|
| 97 |
<xsl:copy-of select='.'/> |
|---|
| 98 |
</xsl:template> |
|---|
| 99 |
|
|---|
| 100 |
<!-- |
|---|
| 101 |
Normalize a table, add some attribute with default values if ommitted and normalize all attribute and childnodes |
|---|
| 102 |
--> |
|---|
| 103 |
<xsl:template match='table'> |
|---|
| 104 |
<table> |
|---|
| 105 |
<xsl:if test='not(boolean(@skipSql))'> |
|---|
| 106 |
<xsl:attribute name='skipSql'>false</xsl:attribute> |
|---|
| 107 |
</xsl:if> |
|---|
| 108 |
<xsl:if test='not(boolean(@abstract))'> |
|---|
| 109 |
<xsl:attribute name='abstract'>false</xsl:attribute> |
|---|
| 110 |
</xsl:if> |
|---|
| 111 |
<xsl:apply-templates select='@*'/> |
|---|
| 112 |
<xsl:apply-templates select='column'/> |
|---|
| 113 |
<xsl:apply-templates select='foreign-key'/> |
|---|
| 114 |
<xsl:apply-templates select='index'/> |
|---|
| 115 |
<xsl:apply-templates select='unique'/> |
|---|
| 116 |
<xsl:apply-templates select='id-method-parameter'/> |
|---|
| 117 |
<xsl:apply-templates select='validator'/> |
|---|
| 118 |
<xsl:apply-templates select='vendor'/> |
|---|
| 119 |
</table> |
|---|
| 120 |
</xsl:template> |
|---|
| 121 |
|
|---|
| 122 |
<!-- |
|---|
| 123 |
Normalize a foreign-key, add some attribute with default values if ommitted and normalize all attribute and childnodes |
|---|
| 124 |
--> |
|---|
| 125 |
<xsl:template match='foreign-key'> |
|---|
| 126 |
<foreign-key> |
|---|
| 127 |
<xsl:if test='not(boolean(@onDelete)) and not(boolean(@OnDelete))'> |
|---|
| 128 |
<xsl:attribute name='onDelete'>none</xsl:attribute> |
|---|
| 129 |
</xsl:if> |
|---|
| 130 |
<xsl:if test='not(boolean(@onUpdate)) and not(boolean(@OnUpdate))'> |
|---|
| 131 |
<xsl:attribute name='onUpdate'>none</xsl:attribute> |
|---|
| 132 |
</xsl:if> |
|---|
| 133 |
<xsl:apply-templates select='@*'/> |
|---|
| 134 |
<xsl:apply-templates select='reference'/> |
|---|
| 135 |
<xsl:apply-templates select='vendor'/> |
|---|
| 136 |
</foreign-key> |
|---|
| 137 |
</xsl:template> |
|---|
| 138 |
|
|---|
| 139 |
<!-- |
|---|
| 140 |
Just copy the index node with attributes and add the index-column |
|---|
| 141 |
--> |
|---|
| 142 |
<xsl:template match='index'> |
|---|
| 143 |
<index> |
|---|
| 144 |
<xsl:apply-templates select='@*'/> |
|---|
| 145 |
<xsl:apply-templates select='index-column'/> |
|---|
| 146 |
</index> |
|---|
| 147 |
</xsl:template> |
|---|
| 148 |
|
|---|
| 149 |
<!-- |
|---|
| 150 |
Just copy the unique node with attributes and add the unique-column |
|---|
| 151 |
--> |
|---|
| 152 |
<xsl:template match='unique'> |
|---|
| 153 |
<unique> |
|---|
| 154 |
<xsl:apply-templates select='@*'/> |
|---|
| 155 |
<xsl:apply-templates select='unique-column'/> |
|---|
| 156 |
</unique> |
|---|
| 157 |
</xsl:template> |
|---|
| 158 |
|
|---|
| 159 |
<!-- |
|---|
| 160 |
Just copy the unique-column node with attributes and add the vendor node |
|---|
| 161 |
--> |
|---|
| 162 |
<xsl:template match='unique-column'> |
|---|
| 163 |
<unique-column> |
|---|
| 164 |
<xsl:apply-templates select='@*'/> |
|---|
| 165 |
<xsl:apply-templates select='vendor'/> |
|---|
| 166 |
</unique-column> |
|---|
| 167 |
</xsl:template> |
|---|
| 168 |
|
|---|
| 169 |
<!-- |
|---|
| 170 |
Just copy the index-column node with attributes and add the vendor node |
|---|
| 171 |
--> |
|---|
| 172 |
<xsl:template match='index-column'> |
|---|
| 173 |
<index-column> |
|---|
| 174 |
<xsl:apply-templates select='@*'/> |
|---|
| 175 |
<xsl:apply-templates select='vendor'/> |
|---|
| 176 |
</index-column> |
|---|
| 177 |
</xsl:template> |
|---|
| 178 |
|
|---|
| 179 |
<!-- |
|---|
| 180 |
Add default name to id-method-parameter (if none) and copy its attributes |
|---|
| 181 |
--> |
|---|
| 182 |
<xsl:template match='id-method-parameter'> |
|---|
| 183 |
<id-method-parameter> |
|---|
| 184 |
<xsl:if test='not(boolean(@name))'> |
|---|
| 185 |
<xsl:attribute name='name'>default</xsl:attribute> |
|---|
| 186 |
</xsl:if> |
|---|
| 187 |
<xsl:apply-templates select='@*'/> |
|---|
| 188 |
</id-method-parameter> |
|---|
| 189 |
</xsl:template> |
|---|
| 190 |
|
|---|
| 191 |
<!-- |
|---|
| 192 |
Just copy the validator node with attributes and add the rule node |
|---|
| 193 |
--> |
|---|
| 194 |
<xsl:template match='validator'> |
|---|
| 195 |
<validator> |
|---|
| 196 |
<xsl:apply-templates select='@*'/> |
|---|
| 197 |
<xsl:apply-templates select='rule'/> |
|---|
| 198 |
</validator> |
|---|
| 199 |
</xsl:template> |
|---|
| 200 |
|
|---|
| 201 |
<!-- |
|---|
| 202 |
Adds a default name to the rule (if none given) and copy the attributes |
|---|
| 203 |
--> |
|---|
| 204 |
<xsl:template match='rule'> |
|---|
| 205 |
<rule> |
|---|
| 206 |
<xsl:if test='not(boolean(@name))'> |
|---|
| 207 |
<xsl:attribute name='name'>class</xsl:attribute> |
|---|
| 208 |
</xsl:if> |
|---|
| 209 |
<xsl:apply-templates select='@*'/> |
|---|
| 210 |
</rule> |
|---|
| 211 |
</xsl:template> |
|---|
| 212 |
|
|---|
| 213 |
<!-- |
|---|
| 214 |
Strip all childnodes (if any) from a parameter node |
|---|
| 215 |
--> |
|---|
| 216 |
<xsl:template match='parameter'> |
|---|
| 217 |
<parameter> |
|---|
| 218 |
<xsl:apply-templates select='@*'/> |
|---|
| 219 |
</parameter> |
|---|
| 220 |
</xsl:template> |
|---|
| 221 |
|
|---|
| 222 |
<!-- |
|---|
| 223 |
Just copy the vendor node with attributes and add the parameter node |
|---|
| 224 |
--> |
|---|
| 225 |
<xsl:template match='vendor'> |
|---|
| 226 |
<vendor> |
|---|
| 227 |
<xsl:apply-templates select='@*'/> |
|---|
| 228 |
<xsl:apply-templates select='parameter'/> |
|---|
| 229 |
</vendor> |
|---|
| 230 |
</xsl:template> |
|---|
| 231 |
|
|---|
| 232 |
<!-- |
|---|
| 233 |
Strip all childnodes from an inheritance node |
|---|
| 234 |
--> |
|---|
| 235 |
<xsl:template match='inheritance'> |
|---|
| 236 |
<inheritance> |
|---|
| 237 |
<xsl:apply-templates select='@*'/> |
|---|
| 238 |
</inheritance> |
|---|
| 239 |
</xsl:template> |
|---|
| 240 |
|
|---|
| 241 |
<!-- |
|---|
| 242 |
Normalize a column node, add default values for missing attributes and copy the content |
|---|
| 243 |
--> |
|---|
| 244 |
<xsl:template match='column'> |
|---|
| 245 |
<column> |
|---|
| 246 |
<xsl:if test='not(boolean(@primaryKey))'> |
|---|
| 247 |
<xsl:attribute name='primaryKey'>false</xsl:attribute> |
|---|
| 248 |
</xsl:if> |
|---|
| 249 |
<xsl:if test='not(boolean(@required))'> |
|---|
| 250 |
<xsl:attribute name='required'>false</xsl:attribute> |
|---|
| 251 |
</xsl:if> |
|---|
| 252 |
<xsl:if test='not(boolean(@type))'> |
|---|
| 253 |
<xsl:attribute name='type'>VARCHAR</xsl:attribute> |
|---|
| 254 |
</xsl:if> |
|---|
| 255 |
<xsl:if test='not(boolean(@autoIncrement))'> |
|---|
| 256 |
<xsl:attribute name='autoIncrement'>false</xsl:attribute> |
|---|
| 257 |
</xsl:if> |
|---|
| 258 |
<xsl:if test='not(boolean(@lazyLoad))'> |
|---|
| 259 |
<xsl:attribute name='lazyLoad'>false</xsl:attribute> |
|---|
| 260 |
</xsl:if> |
|---|
| 261 |
<xsl:if test='@type = "VARCHAR" and not(boolean(@sqlType)) and not(boolean(@size))'> |
|---|
| 262 |
<xsl:attribute name='size'>255</xsl:attribute> |
|---|
| 263 |
</xsl:if> |
|---|
| 264 |
<xsl:apply-templates select='@*'/> |
|---|
| 265 |
<xsl:apply-templates select='inheritance'/> |
|---|
| 266 |
<xsl:apply-templates select='vendor'/> |
|---|
| 267 |
</column> |
|---|
| 268 |
</xsl:template> |
|---|
| 269 |
|
|---|
| 270 |
<!-- |
|---|
| 271 |
Strip all childnodes from an reference node |
|---|
| 272 |
--> |
|---|
| 273 |
<xsl:template match='reference'> |
|---|
| 274 |
<reference> |
|---|
| 275 |
<xsl:apply-templates select='@*'/> |
|---|
| 276 |
</reference> |
|---|
| 277 |
</xsl:template> |
|---|
| 278 |
|
|---|
| 279 |
</xsl:stylesheet> |
|---|