| 1 |
<?xml version="1.0" encoding="iso-8859-1"?> |
| 2 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 3 |
<head> |
| 4 |
<title>Converting RELAX NG to XSD</title> |
| 5 |
<style type="text/css"> |
| 6 |
body { font-family: Arial } |
| 7 |
h1 { text-align: center } |
| 8 |
h2 { text-align: center } |
| 9 |
p { font-size: larger } |
| 10 |
div.group { margin-left: 40pt } |
| 11 |
pre { font-size: larger; font-family: monospace; margin-left: 20pt } |
| 12 |
code { font-size: larger; font-family: monospace } |
| 13 |
</style></head> |
| 14 |
<body> |
| 15 |
<p>These are the slides for a talk given at the XML 2002 |
| 16 |
conference in Baltimore. They have been |
| 17 |
combined into a single HTML file.</p> |
| 18 |
<hr /> |
| 19 |
<h1>Converting RELAX NG to XSD</h1> |
| 20 |
<h2>James Clark</h2> |
| 21 |
<hr /> |
| 22 |
<h1>Goals</h1> |
| 23 |
|
| 24 |
<p>Good-quality XSD</p> |
| 25 |
<p>Structure (define/include) preserving</p> |
| 26 |
<p>Approximate where necessary</p> |
| 27 |
<p>Useful rather than perfect</p> |
| 28 |
|
| 29 |
<hr /> |
| 30 |
<h1>Conversion strategy</h1> |
| 31 |
|
| 32 |
<p>Build RELAX NG object model</p> |
| 33 |
<p>Convert RELAX NG object model to intermediate form</p> |
| 34 |
<p>Perform transformations on intermediate form</p> |
| 35 |
<p>Generate XSD from intermediate form</p> |
| 36 |
|
| 37 |
<hr /> |
| 38 |
<h1>Intermediate form</h1> |
| 39 |
|
| 40 |
<p>Schema language between RELAX NG and XSD</p> |
| 41 |
<p>Abstract, no syntax</p> |
| 42 |
<p>No mixed element/attribute content models</p> |
| 43 |
<p>Clean, simple semantics</p> |
| 44 |
<p>Schema structure more controlled than RELAX NG</p> |
| 45 |
|
| 46 |
<hr /> |
| 47 |
<h1>Intermediate schema components</h1> |
| 48 |
|
| 49 |
<p><i>Simple type definition</i> associates local name with simple |
| 50 |
type</p> |
| 51 |
|
| 52 |
<p><i>Attribute group definition</i> associates local name with |
| 53 |
attribute use</p> |
| 54 |
|
| 55 |
<p><i>Group definition</i> associates local name with particle</p> |
| 56 |
|
| 57 |
<p><i>Start declaration</i> declares particle that document |
| 58 |
element must match</p> |
| 59 |
|
| 60 |
<p><i>Include</i> references a schema</p> |
| 61 |
|
| 62 |
<hr /> |
| 63 |
<h1>Points to note</h1> |
| 64 |
|
| 65 |
<p>Order of components not semantically significant</p> |
| 66 |
|
| 67 |
<p>Simple type definitions, attribute group definitions, group |
| 68 |
definitions have distinct symbol spaces</p> |
| 69 |
|
| 70 |
<p>Definitions are named with local names not QNames</p> |
| 71 |
|
| 72 |
<p>Builtin simple types do not have simple type definitions</p> |
| 73 |
|
| 74 |
<p>No target namespace associated with a schema</p> |
| 75 |
|
| 76 |
<p>No complex type declarations, element declarations or attribute |
| 77 |
declarations</p> |
| 78 |
|
| 79 |
<hr /> |
| 80 |
<h1>Simple types</h1> |
| 81 |
|
| 82 |
<p><i>Restriction</i> contains the name of builtin simple |
| 83 |
type and list of facets</p> |
| 84 |
|
| 85 |
<p><i>List</i> contains a simple type |
| 86 |
and a minimum/maximum number of occurrences</p> |
| 87 |
|
| 88 |
<p><i>Union</i> contains a list of simple types</p> |
| 89 |
|
| 90 |
<p><i>Reference</i> contains a local name referring to a simple |
| 91 |
type definition</p> |
| 92 |
|
| 93 |
<hr /> |
| 94 |
<h1>Particles</h1> |
| 95 |
|
| 96 |
<p><i>Element</i> contains an expanded QName |
| 97 |
and a complex type</p> |
| 98 |
|
| 99 |
<p><i>Wildcard element</i> contains a wildcard</p> |
| 100 |
|
| 101 |
<p><i>Repeat</i> contains a particle and a minimum/maximum |
| 102 |
number of occurrences</p> |
| 103 |
|
| 104 |
<p><i>Sequence</i> contains one or more particles</p> |
| 105 |
|
| 106 |
<p><i>Choice</i> contains one or more particles</p> |
| 107 |
|
| 108 |
<p><i>Interleave</i> contains one or more particles</p> |
| 109 |
|
| 110 |
<p><i>Reference</i> contains a local name referring to a group |
| 111 |
definition</p> |
| 112 |
|
| 113 |
<hr /> |
| 114 |
|
| 115 |
<h1>Complex types</h1> |
| 116 |
|
| 117 |
<p><i>Complex content</i> contains attribute use, |
| 118 |
a particle, a mixed flag</p> |
| 119 |
|
| 120 |
<p><i>Simple content</i> contains attribute use, |
| 121 |
a simple type</p> |
| 122 |
|
| 123 |
<hr /> |
| 124 |
<h1>Attribute uses</h1> |
| 125 |
|
| 126 |
<p><i>Attribute</i> contains an expanded QName and a simple type</p> |
| 127 |
|
| 128 |
<p><i>Optional attribute</i> contains an attribute and a default |
| 129 |
value</p> |
| 130 |
|
| 131 |
<p><i>Wildcard attribute</i> contains a wildcard</p> |
| 132 |
|
| 133 |
<p><i>Attribute group</i> contains a list of zero or more |
| 134 |
attributes</p> |
| 135 |
|
| 136 |
<p><i>Attribute use choice</i> contains a list of one or |
| 137 |
more attribute uses</p> |
| 138 |
|
| 139 |
<p><i>Reference</i> contains a local name referring to an |
| 140 |
attribute group definition</p> |
| 141 |
|
| 142 |
<hr /> |
| 143 |
|
| 144 |
<h1>Wildcard</h1> |
| 145 |
|
| 146 |
<p>Positive/negative flag</p> |
| 147 |
<p>Set of namespace URIs</p> |
| 148 |
<p>Set of excluded expanded QNames</p> |
| 149 |
|
| 150 |
<hr /> |
| 151 |
<h1>Conversion from RELAX NG to intermediate form</h1> |
| 152 |
<hr /> |
| 153 |
<h1>Pattern analysis</h1> |
| 154 |
|
| 155 |
<p>Flags computed based on possible matches of the pattern</p> |
| 156 |
|
| 157 |
<div class="group"> |
| 158 |
|
| 159 |
<p><i>empty</i> says if there is a match whose content is empty</p> |
| 160 |
|
| 161 |
<p><i>text</i> says if there is a match whose content includes a |
| 162 |
text node that is matched against a text pattern</p> |
| 163 |
|
| 164 |
<p><i>data</i> says if there is a match whose content includes a |
| 165 |
text node that is matched against a data, value or list |
| 166 |
pattern</p> |
| 167 |
|
| 168 |
<p><i>attribute</i> says if there is a match that includes an |
| 169 |
attribute</p> |
| 170 |
|
| 171 |
<p><i>element</i> says if there a match whose content includes an |
| 172 |
element</p> |
| 173 |
|
| 174 |
</div> |
| 175 |
|
| 176 |
<p>Sufficient to allow conversion to intermediate form</p> |
| 177 |
<p>Can compute flags for patterns from subpatterns</p> |
| 178 |
|
| 179 |
<hr /> |
| 180 |
<h1>Converting patterns</h1> |
| 181 |
|
| 182 |
<p>A pattern can be converted in three ways:</p> |
| 183 |
|
| 184 |
<div class="group"> |
| 185 |
<p>A pattern may be converted to a particle</p> |
| 186 |
<p>A pattern may be converted to a simple type</p> |
| 187 |
<p>A pattern may be converted to an attribute use</p> |
| 188 |
</div> |
| 189 |
|
| 190 |
<p>A single pattern may be converted both to a particle or a |
| 191 |
simple type and to an attribute use</p> |
| 192 |
|
| 193 |
<p><code>element</code> patterns are treated like |
| 194 |
<code>empty</code> when converting to an attribute use</p> |
| 195 |
|
| 196 |
<p><code>attribute</code> patterns are treated like |
| 197 |
<code>empty</code> when converting to a particle or simple |
| 198 |
type</p> |
| 199 |
|
| 200 |
<hr /> |
| 201 |
<h1>Converting name classes</h1> |
| 202 |
|
| 203 |
<p>A name class is converted to:</p> |
| 204 |
|
| 205 |
<div class="group"> |
| 206 |
<p>a set of expanded QNames</p> |
| 207 |
<p>a wildcard</p> |
| 208 |
</div> |
| 209 |
|
| 210 |
<hr /> |
| 211 |
|
| 212 |
<h1>Converting an element pattern to a particle</h1> |
| 213 |
|
| 214 |
<p>Split name class into wildcard and list of expanded names</p> |
| 215 |
|
| 216 |
<p>Generate wildcard element particle for wildcard</p> |
| 217 |
|
| 218 |
<p>Generate element particle for each expanded name by converting |
| 219 |
body of element pattern to a complex type</p> |
| 220 |
|
| 221 |
<p>Combine with choice particle</p> |
| 222 |
|
| 223 |
<hr /> |
| 224 |
|
| 225 |
<h1>Converting element body to a complex type</h1> |
| 226 |
|
| 227 |
<p>If body has <i>element</i> flag, then use a complex type with |
| 228 |
complex content and convert body to a particle</p> |
| 229 |
|
| 230 |
<p>Mixed if body has either <i>data</i> or <i>text</i> flag</p> |
| 231 |
|
| 232 |
<p>If body has <i>data</i> flag but neither <i>text</i> nor |
| 233 |
<i>element</i> flag, then use a complex type with simple |
| 234 |
content and convert body to a simple type</p> |
| 235 |
|
| 236 |
<p>In addition, convert body to an attribute use</p> |
| 237 |
|
| 238 |
<hr /> |
| 239 |
|
| 240 |
<h1>Converting a define</h1> |
| 241 |
|
| 242 |
<p>If body has <i>attribute</i> flag, then generate an attribute |
| 243 |
group definition by converting body to an attribute use</p> |
| 244 |
|
| 245 |
<p>If body has <i>element</i> flag, then generate a group definition |
| 246 |
by converting body to a particle</p> |
| 247 |
|
| 248 |
<p>If body has <i>data</i> flag but neither <i>text</i> nor |
| 249 |
<i>element</i> flag, then generate a simple type definition by |
| 250 |
converting body to simple type</p> |
| 251 |
|
| 252 |
<hr /> |
| 253 |
<h1>Converting a list</h1> |
| 254 |
|
| 255 |
<p>Intermediate form like XSD not RELAX NG</p> |
| 256 |
<p>Compute minimum and maximum number of tokens in list</p> |
| 257 |
<p>Compute union of simple types of possible members of list</p> |
| 258 |
|
| 259 |
<hr /> |
| 260 |
<h1>Transformations on intermediate form</h1> |
| 261 |
|
| 262 |
<p>Transform out attribute choice</p> |
| 263 |
<p>Transform out interleave except where XSD allows it</p> |
| 264 |
<p>Combine attribute wildcards</p> |
| 265 |
<p>Combine unions of simple types with enumeration facet</p> |
| 266 |
|
| 267 |
<hr /> |
| 268 |
<h1>Conversion from intermediate form into XSD</h1> |
| 269 |
<hr /> |
| 270 |
<h1>Namespace analysis</h1> |
| 271 |
|
| 272 |
<p>Assign target namespace to each file in intermediate schema</p> |
| 273 |
<p>Choose or create principal file for every namespace</p> |
| 274 |
<p>Determine which attributes, element particles need |
| 275 |
to be moved</p> |
| 276 |
<p>Determine which negative wildcards need to be moved</p> |
| 277 |
<p>Determine which attributes, element particles should be global</p> |
| 278 |
<p>Null namespace needs special treatment</p> |
| 279 |
|
| 280 |
<hr /> |
| 281 |
<h1>Complex type analysis</h1> |
| 282 |
|
| 283 |
<p>Identify cases where complex type can be used instead of</p> |
| 284 |
|
| 285 |
<div class="group"> |
| 286 |
<p>Simple type definition and optionally attribute group definition, or</p> |
| 287 |
<p>Group definition and optionally attribute group definition</p> |
| 288 |
</div> |
| 289 |
|
| 290 |
<p>All references must be such that they can turn into</p> |
| 291 |
|
| 292 |
<div class="group"> |
| 293 |
<p>the type of an element</p> |
| 294 |
<p>the base type of a complex type extension</p> |
| 295 |
</div> |
| 296 |
|
| 297 |
<hr /> |
| 298 |
<h1>XSD output</h1> |
| 299 |
|
| 300 |
<p>Take advantage of XSD shorthands</p> |
| 301 |
|
| 302 |
<p>Generate complex type definitions</p> |
| 303 |
|
| 304 |
<p>Generate global element/attribute declarations</p> |
| 305 |
|
| 306 |
<p>Generate bridging definitions for non-global moved |
| 307 |
elements/attributes</p> |
| 308 |
|
| 309 |
<p>Generate bridging definitions for negative wildcards</p> |
| 310 |
|
| 311 |
<p>Deal with attribute wildcards</p> |
| 312 |
|
| 313 |
<hr /> |
| 314 |
<h1>Possible improvements</h1> |
| 315 |
|
| 316 |
<p>Avoid violating <i>unique particle attribution</i> constraint</p> |
| 317 |
<p>Avoid violating <i>element declarations consistent</i> constraint</p> |
| 318 |
<p>Take advantage of substitution groups</p> |
| 319 |
<p>Better handling of interleave</p> |
| 320 |
<p>Inform user about all approximations</p> |
| 321 |
<p>Generate annotations using eg Schematron to make approximations exact</p> |
| 322 |
<p>Handle RELAX NG overrides using redefine</p> |
| 323 |
|
| 324 |
<hr /> |
| 325 |
<h1>Implementation</h1> |
| 326 |
|
| 327 |
<p>Trang (Translator for RELAX NG Schemas)</p> |
| 328 |
<p>Open source</p> |
| 329 |
<p>http://www.thaiopensource.com/relaxng/trang.html</p> |
| 330 |
|
| 331 |
</body> |
| 332 |
</html> |