ch16.xml 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <?xml-stylesheet type="text/css" href="chapter.css"?><chapter>
  3. <title>Chapter 16: Determinism and Datatype Assignment</title>
  4. <simplesect>
  5. <para>One of the strengths of Relax NG is its flexibility in supporting scaring concepts called &quot;ambiguous content models&quot; (SGML world) &quot;non-deterministic content models&quot; (XML DTDs) or &quot;Unique Particle Attribution rule&quot; and &quot;Consistent Declaration rule&quot; (W3C XML Schema). </para>
  6. <para>Before you read on into this chapter, let's make it clear that as far as validation only is concerned, it's perfectly fine with Relax NG to write ambiguous schemas. </para>
  7. <para>That being said, when type assignment or data binding is involved, ambiguity may become a problem and we will see in this chapter how Relax NG can be used to be &quot;type assignment friendly&quot;.</para>
  8. </simplesect>
  9. <sect1>
  10. <title>What are we talking about?</title>
  11. <para>The thing we need to do first is to try to clarified these notions which are blurred in many papers and discussions and are not as obscure as people often think.</para>
  12. <sect2>
  13. <title>Ambiguity versus determinism</title>
  14. <para>The first distinction to make is to differentiate what's called ambiguity (or rather unambiguity) and what's called determinism. These two terms have been given precise definitions by regular expressions and hedge grammars theoreticians and part of the confusion around these notions comes from the fact that they are often misused.</para>
  15. <para>A schema is said to be ambiguous when a document may be valid through different pattern alternatives. A trivial example is:</para>
  16. <programlisting>
  17. <![CDATA[ element foo{empty} | element foo{empty}]]>
  18. </programlisting>
  19. <para>When an empty element named <literal>foo</literal> is found in an instance document, there is no way to say if it is valid per the left or right definition of &quot;element foo{empty}&quot; in the schema.</para>
  20. <para>There are, of course, more complex cases of ambiguity and we'll see some of them in the next sections, but this is the general idea behind ambiguity.</para>
  21. <para>Ambiguity (or unambiguity) is independent of any implementation or algorithm. It's a property of the schema itself and without rewriting it a schema is either ambiguous or not.</para>
  22. <para>On the contrary, determinism has been introduced to facilitate implementation of schema processors. The basic idea beyond determinism is that at each point when matching an instance document against a schema, the schema processor has at most one possible choice. This is making the life easier for implementers which can safely rely on well known algorithm such as automatons (also called Finite State Machines or FSMs) and be sure that their computation times will not grow up exponentially. This is also a major constraint imposed on schema authors.</para>
  23. <para>An ambiguous schema is always non deterministic, but the opposite is far from being true. Consider for instance:</para>
  24. <programlisting>
  25. <![CDATA[ element foo{empty} | (element foo{empty}, element bar{empty})]]>
  26. </programlisting>
  27. <para>This is not ambiguous since after having read the element after an empty element named <literal>foo</literal> a schema processor is able to say if the right or left alternative is being used (or none if the document is invalid) but this is non deterministic since when a schema processor is matching an empty element named <literal>foo</literal> it has two different choices and cannot choose between them without looking ahead at the next element.</para>
  28. <para>Ambiguous schemas are not a problem as long as validation only is concerned: their validation reports are consistent and we don't care why a document is valid or not as long as the answer (valid or invalid) is reliable. The only real downside about ambiguous schemas is for applications performing datatype assignment (or more generally instance document annotation) through validation and we will see more about these issues in the next sections of this chapter.</para>
  29. <para>The main issue with schema languages requiring deterministic schemas is that some content models are fundamentally non deterministic and cannot be rewritten in a deterministic form. Such schema languages are not only adding restrictions on the forms to use to write a schema but their expressive power is limited and they cannot describe all the content models allowed in well formed XML. We will see examples of content models impossible to describe in a deterministic form in the section about compatibility with W3C XML Schema.</para>
  30. </sect2>
  31. <sect2>
  32. <title>Different types of ambiguities</title>
  33. <para>In a Relax NG schema, we can distinguish four different types of ambiguities: regular expression ambiguities, hedge grammar ambiguities, name classes ambiguity and datatype ambiguities and we'll briefly introduce them since they have slightly different behaviors.</para>
  34. <sect3>
  35. <title>Regular expression ambiguities</title>
  36. <para>Note that in this chapter we are using the term &quot;regular expression&quot; as used in the math behind Relax NG. The term &quot;regular expression&quot; that you'll find in this chapter should thus not be confused with the regular expressions which we've seen in the W3C XML Schema <literal>pattern</literal> facet.</para>
  37. <para>After a schema has been simplified, we can make a clear distinction between the definition of each element (embedded in its own named pattern) and the grammar which combines these definitions. What's called a regular expression ambiguity is an ambiguity which resides within the definition of an element.</para>
  38. <para>Theoreticians have demonstrated that any ambiguous regular expressions may be rewritten in an unambiguous way and these ambiguities may be considered as unlucky variations over unambiguous schemas.</para>
  39. <para>A basic example of such a choice between a pattern and itself is:</para>
  40. <programlisting>
  41. <![CDATA[ <choice>
  42. <ref name="pattern"/>
  43. <ref name="pattern"/>
  44. </choice>]]>
  45. </programlisting>
  46. <para>or:</para>
  47. <programlisting>
  48. <![CDATA[ pattern|pattern]]>
  49. </programlisting>
  50. <para>Obvious in this case, the unambiguous form is more or less difficult to find when the ambiguous pattern gets more complex. For instance, the following pattern:</para>
  51. <programlisting>
  52. <![CDATA[ <choice>
  53. <group>
  54. <optional>
  55. <ref name="first"/>
  56. </optional>
  57. <ref name="second"/>
  58. </group>
  59. <group>
  60. <ref name="second"/>
  61. <optional>
  62. <ref name="third"/>
  63. </optional>
  64. </group>
  65. </choice>]]>
  66. </programlisting>
  67. <para>or:</para>
  68. <programlisting>
  69. <![CDATA[ (first?,second)|(second,third?)]]>
  70. </programlisting>
  71. <para>Is ambiguous because an instance nodeset matching only the named pattern <literal>second</literal> without the leading <literal>first</literal> nor the ending <literal>third</literal> is valid per the two alternative of the choice. It can be rewritten by removing the option of matching only the <literal>second</literal> pattern from one of the alternatives:</para>
  72. <para>or:</para>
  73. <programlisting>
  74. <![CDATA[ <group>
  75. <optional>
  76. <ref name="first"/>
  77. </optional>
  78. <ref name="second"/>
  79. </group>
  80. <group>
  81. <ref name="second"/>
  82. <ref name="third"/>
  83. </group>
  84. </choice>]]>
  85. </programlisting>
  86. <para>or:</para>
  87. <programlisting>
  88. <![CDATA[ (first?,second)|(second,third)]]>
  89. </programlisting>
  90. <para>Algorithms have been developed to rewrite ambiguous regular expressions in their unambiguous forms and it would be really useful if XML development tools could implement them to propose non ambiguous alternatives for ambiguous patterns when they exist. Until this happens, the best thing to do when you are confronted with an ambiguous pattern to disambiguate is to take a step back, grab a cup of tea or coffee and calmly write the different combinations expressed by the schema to combine them differently till the combination isn't ambiguous any longer.</para>
  91. <para>Note that explicit choices aren't the only pattern which can lead to ambiguous schemas. Consider this simple pattern:</para>
  92. <programlisting>
  93. <![CDATA[ <group>
  94. <optional>
  95. <ref name="pattern"/>
  96. </optional>
  97. <optional>
  98. <ref name="pattern"/>
  99. </optional>
  100. <group>]]>
  101. </programlisting>
  102. <para>or:</para>
  103. <programlisting>
  104. <![CDATA[ pattern?, pattern?]]>
  105. </programlisting>
  106. <para>If we have a content model which matches only one pattern, we cannot know if it will match it for the first or the second occurrence of the pattern and this schema can be considered as ambiguous. To rewrite it as a non ambiguous schema, we could write:</para>
  107. <programlisting>
  108. <![CDATA[ <optional>
  109. <ref name="pattern"/>
  110. <optional>
  111. <ref name="pattern"/>
  112. </optional>
  113. </optional>]]>
  114. </programlisting>
  115. <para>or:</para>
  116. <programlisting>
  117. <![CDATA[ (pattern, pattern?)?]]>
  118. </programlisting>
  119. <para>Although the way leading to rewritings may look opaque, the math behind Relax NG can help us like high school algebra helps us factorize mathematical expressions. As an exercise, let's decompose the chain of factorizations and simplifications to rewrite &quot;pattern?, pattern?&quot; as &quot;(pattern, pattern?)?&quot;.</para>
  120. <para>The first step relies on the fact that &quot;pattern?&quot; is equivalent to &quot;empty|pattern&quot;:</para>
  121. <programlisting>
  122. <![CDATA[ pattern?, pattern?]]>
  123. </programlisting>
  124. <para>is equivalent to:</para>
  125. <programlisting>
  126. <![CDATA[ (empty|pattern), (empty|pattern)]]>
  127. </programlisting>
  128. <para>which can be factorized as:</para>
  129. <programlisting>
  130. <![CDATA[ (empty,empty)|(empty,pattern)|(pattern,empty)|(pattern,pattern)]]>
  131. </programlisting>
  132. <para>which can be simplified as:</para>
  133. <programlisting>
  134. <![CDATA[ empty|pattern|(pattern,pattern)]]>
  135. </programlisting>
  136. <para>which is equivalent to:</para>
  137. <programlisting>
  138. <![CDATA[ empty|(pattern,(empty|pattern))]]>
  139. </programlisting>
  140. <para>which is equivalent to</para>
  141. <programlisting>
  142. <![CDATA[ (pattern, pattern?)?]]>
  143. </programlisting>
  144. <para>We could argue whether the unambiguous forms are clearer, more logical an easier to read than the ambiguous forms or not, but I think that the answer would be very subjective. These different forms are highly dependent of the perspective from which we have analyzed the content of instance documents. There isn't a good nor a bad form and working with a schema language such as Relax NG which supports all of these forms does save a lot of time: you don't have to take a perspective imposed by the language! </para>
  145. <para>A last thing to note is that disambiguating regular expressions does not significantly change the structure or the style of your schema since the changes are limited to the regular expression itself and this will not be the case of ambiguous regular hedge grammars.</para>
  146. </sect3>
  147. <sect3>
  148. <title>Ambiguous regular hedge grammars</title>
  149. <para>In the case of a Relax NG schema, we've defined a regular expression ambiguity as an ambiguity which resides within the definition of an element. Ambiguous regular hedge grammars are on the contrary ambiguities spread over element definitions which play the role of &quot;hedges&quot; in a Relax NG schema. A example of an ambiguous regular hedge grammar is:</para>
  150. <programlisting>
  151. <![CDATA[ <choice>
  152. <ref name="pattern1"/>
  153. <ref name="pattern2"/>
  154. </choice>
  155. .../...
  156. <define name="pattern1">
  157. <element name="foo">
  158. <empty/>
  159. </element>
  160. </define>
  161. <define name="pattern2">
  162. <element name="foo">
  163. <empty/>
  164. </element>
  165. </define>]]>
  166. </programlisting>
  167. <para>or:</para>
  168. <programlisting>
  169. <![CDATA[ pattern1|pattern2
  170. .../...
  171. pattern1=element foo{empty}
  172. pattern2=element foo{empty}]]>
  173. </programlisting>
  174. <para>This example is ambiguous because when we find an empty element <literal>foo</literal> we can't tell if it's been validated through <literal>pattern1</literal> or <literal>pattern2</literal> and it's an ambiguous hedge grammar (rather than an ambiguous regular expression) because the ambiguity is spread over two hedges, i.e. two definitions of the element <literal>foo</literal>.</para>
  175. <para>Again, it has been demonstrated that ambiguous regular hedge grammars can be rewritten in unambiguous forms but the disambiguation must be done at the level of the grammar itself and does often requires heavy changes to the structure of the schema.</para>
  176. <para>The exercise of disambiguating regular hedge grammars can get significantly complicated when compositions of named patterns and grammars are involved. For instance, maintaining non ambiguous patterns while combining definitions by choice means that you need to exclude all the instance nodesets valid per the original definition from the pattern given as a choice and this isn't always possible without modifying the included schema. Consider for instance this pattern:</para>
  177. <programlisting>
  178. <![CDATA[ <define name="namedPattern">
  179. <ref name="first"/>
  180. </define>]]>
  181. </programlisting>
  182. <para>or:</para>
  183. <programlisting>
  184. <![CDATA[ namedPattern=first]]>
  185. </programlisting>
  186. <para>If we need to add an optional <literal>second</literal> pattern it may seem natural to combine it by choice as:</para>
  187. <programlisting>
  188. <![CDATA[ <define name="namedPattern" combine="choice">
  189. <ref name="first"/>
  190. <optional>
  191. <ref name="second"/>
  192. </optional>
  193. </define>]]>
  194. </programlisting>
  195. <para>or:</para>
  196. <programlisting>
  197. <![CDATA[ namedPattern|=first,second?]]>
  198. </programlisting>
  199. <para>The result of the combination is equivalent to:</para>
  200. <programlisting>
  201. <![CDATA[ <define name="namedPattern">
  202. <choice>
  203. <ref name="first"/>
  204. <group>
  205. <ref name="first"/>
  206. <optional>
  207. <ref name="second"/>
  208. </optional>
  209. </group>
  210. </choice>
  211. </define>]]>
  212. </programlisting>
  213. <para>or:</para>
  214. <programlisting>
  215. <![CDATA[ namedPattern=first|(first,second?)]]>
  216. </programlisting>
  217. <para>This gives us an ambiguous pattern. Of course, outside the context of a pattern combination, this would be trivial to rewrite as:</para>
  218. <programlisting>
  219. <![CDATA[ <define name="namedPattern">
  220. <ref name="first"/>
  221. <optional>
  222. <ref name="second"/>
  223. </optional>
  224. </define>]]>
  225. </programlisting>
  226. <para>or:</para>
  227. <programlisting>
  228. <![CDATA[ namedPattern=first,second?]]>
  229. </programlisting>
  230. <para>but in this case, we won't get there directly by pattern combination and we need to take the problem under a different angle and consider that we must leave in the alternative to the original definition only things which would are not already allowed. In other words, we need to remove from our target of &quot;the first pattern followed by an optional second pattern&quot; the case where the first pattern is not followed by the second one. The alternative will thus be between the first pattern alone and the first pattern followed by a second one:</para>
  231. <programlisting>
  232. <![CDATA[ <define name="namedPattern" combine="choice">
  233. <choice>
  234. <ref name="first"/>
  235. <group>
  236. <ref name="first"/>
  237. <ref name="second"/>
  238. </group>
  239. </choice>
  240. </define>]]>
  241. </programlisting>
  242. <para>or:</para>
  243. <programlisting>
  244. <![CDATA[ namedPattern=first|(first,second)]]>
  245. </programlisting>
  246. <para>With this target in mind, we can rewrite our combination as:</para>
  247. <programlisting>
  248. <![CDATA[ <define name="namedPattern" combine="choice">
  249. <ref name="first"/>
  250. <ref name="second"/>
  251. </define>]]>
  252. </programlisting>
  253. <para>or:</para>
  254. <programlisting>
  255. <![CDATA[ namedPattern|=first,second]]>
  256. </programlisting>
  257. <para>If we want to avoid ambiguous hedge grammars, we also need to be careful when combining named patterns by choice: without knowing how pattern1 and pattern2 are defined, it's just impossible to say if:</para>
  258. <programlisting>
  259. <![CDATA[ <choice>
  260. <ref name="pattern1"/>
  261. <ref name="pattern2"/>
  262. </choice>]]>
  263. </programlisting>
  264. <para>or:</para>
  265. <programlisting>
  266. <![CDATA[ pattern1|pattern2]]>
  267. </programlisting>
  268. <para>is ambiguous or not.</para>
  269. </sect3>
  270. <sect3>
  271. <title>Name class ambiguity</title>
  272. <para>Another source of ambiguity is when name classes used in different alternatives of a choice overlap. Again, a schematic example of such overlap would be:</para>
  273. <programlisting>
  274. <![CDATA[ <choice>
  275. <element name="foo">
  276. <empty/>
  277. </element>
  278. <element>
  279. <anyName/>
  280. <empty/>
  281. </element>
  282. </choice>]]>
  283. </programlisting>
  284. <para>or:</para>
  285. <programlisting>
  286. <![CDATA[ element foo{empty} | element * {empty}]]>
  287. </programlisting>
  288. <para>This is ambiguous since the name class <literal>anyName</literal> includes the name class matching only the name <literal>foo</literal> and an element <literal>foo</literal> would be valid per both branches of the choice pattern.</para>
  289. <para>The <literal>except</literal> name class does save our lives for name class ambiguity since it lets us remove the overlap from one of the alternatives and this pattern can easily be rewritten as a non ambiguous choice pattern:</para>
  290. <programlisting>
  291. <![CDATA[ <choice>
  292. <element name="foo">
  293. <empty/>
  294. </element>
  295. <element>
  296. <anyName>
  297. <except>
  298. <name>foo</name>
  299. </except>
  300. </anyName>
  301. <empty/>
  302. </element>
  303. </choice>]]>
  304. </programlisting>
  305. <para>or:</para>
  306. <programlisting>
  307. <![CDATA[ element foo{empty} | element * - foo {empty}]]>
  308. </programlisting>
  309. <para>Or more simply:</para>
  310. <programlisting>
  311. <![CDATA[ <element>
  312. <anyName>
  313. <except>
  314. <name>foo</name>
  315. </except>
  316. </anyName>
  317. <empty/>
  318. </element>]]>
  319. </programlisting>
  320. <para>or:</para>
  321. <programlisting>
  322. <![CDATA[ element * {empty}]]>
  323. </programlisting>
  324. <para>Note that the fact that name classes overlap is not enough to make an ambiguous pattern. For instance:</para>
  325. <programlisting>
  326. <![CDATA[ <choice>
  327. <element name="foo">
  328. <attribute name="bar">
  329. <empty/>
  330. </attribute>
  331. </element>
  332. <element>
  333. <anyName/>
  334. <empty/>
  335. </element>
  336. </choice>]]>
  337. </programlisting>
  338. <para>or:
  339. element foo{attribute bar{empty}} |
  340. element * {empty}}</para>
  341. <para>is no ambiguous since the content model of the elements with the two name class do not overlap. Making our <literal>bar</literal> attribute optional:</para>
  342. <programlisting>
  343. <![CDATA[ <choice>
  344. <element name="foo">
  345. <optional>
  346. <attribute name="bar">
  347. <empty/>
  348. </attribute>
  349. </optional>
  350. </element>
  351. <element>
  352. <anyName/>
  353. <empty/>
  354. </element>
  355. </choice>]]>
  356. </programlisting>
  357. <para>or:
  358. element foo{attribute bar{empty}?} |
  359. element * {empty}}</para>
  360. <para>is enough to make our pattern ambiguous. However, this pattern is strictly equivalent to the preceding one which means that we know how to rewrite it in a non ambiguous way.</para>
  361. <para>Finally, note that name class ambiguity may be considered as an extension to regular hedge grammar ambiguity. When we have been writing:</para>
  362. <programlisting>
  363. <![CDATA[ element foo{empty} | element foo{empty}]]>
  364. </programlisting>
  365. <para>which after simplification is an example of regular hedge grammar ambiguity, the ambiguity comes from the fact that the name classes for both alternatives are the single value <literal>foo</literal> and thus do overlap.</para>
  366. </sect3>
  367. <sect3>
  368. <title>Ambiguous datatypes</title>
  369. <para>Datatype ambiguity is the one which is the most difficult to handle with Relax NG and that doesn't come from Relax NG itself but rather from the fact that datatype libraries are not built-in and are kind of opaque and less flexible than other patterns or name classes.</para>
  370. <para>A basic example of ambiguous datatypes is:</para>
  371. <programlisting>
  372. <![CDATA[ <element name="foo">
  373. <choice>
  374. <data type="boolean"/>
  375. <data type="integer"/>
  376. </choice>
  377. </element>]]>
  378. </programlisting>
  379. <para>or:</para>
  380. <programlisting>
  381. <![CDATA[ element foo{xsd:boolean|xsd:integer}]]>
  382. </programlisting>
  383. <para>Since the lexical space of the two possible datatypes do overlap (0 and 1 are valid W3C XML Schema boolean and integers), there is no way to determine what is the datatype an element foo with a value <literal>0</literal> or <literal>1</literal>. We have no except pattern available to remove the lexical space of a datatype from the lexical space of another datatype and, the only way to disambiguate such pattern is using parameters when the datatype library provides any. In the case of W3C XML Schema, the parameter to use if we want to work on the lexical space is the pattern parameter and we could remove <literal>0</literal> and <literal>1</literal> from the lexical space of the boolean datatype by either specifying the lexical space of boolean as being explicitly <literal>true</literal> or <literal>false</literal>:</para>
  384. <programlisting>
  385. <![CDATA[ <element name="foo">
  386. <choice>
  387. <data type="boolean">
  388. <param name="pattern">true|false</param>
  389. </data>
  390. <data type="integer"/>
  391. </choice>
  392. </element>]]>
  393. </programlisting>
  394. <para>or:</para>
  395. <programlisting>
  396. <![CDATA[ element foo{
  397. xsd:boolean{pattern="true|false"}
  398. |xsd:integer
  399. }]]>
  400. </programlisting>
  401. <para>Or by removing the lexical space of integer from boolean:</para>
  402. <programlisting>
  403. <![CDATA[ <element name="foo">
  404. <choice>
  405. <data type="boolean">
  406. <param name="pattern">[^0-9]*</param>
  407. </data>
  408. <data type="integer"/>
  409. </choice>
  410. </element>]]>
  411. </programlisting>
  412. <para>or:</para>
  413. <programlisting>
  414. <![CDATA[ element foo{
  415. xsd:boolean{pattern="[^0-9]*"}
  416. |xsd:integer
  417. }]]>
  418. </programlisting>
  419. <para>That's not much fun for more complex cases, but that's the only hope we have to disambiguate such ambiguities.</para>
  420. </sect3>
  421. </sect2>
  422. </sect1>
  423. <sect1>
  424. <title>The downsides of ambiguous and non deterministic content models</title>
  425. <para>Again, if you're only interested in using a Relax NG schema for validation which, after all, is the primary goal of Relax NG, it is perfectly fine to design and use non deterministic and even ambiguous schemas. The downsides of ambiguous schemas appear when we want to use Relax NG schemas for adding validation information to the instance documents or use a Relax NG schema for guided editing and the downsides of non deterministic schemas only appear when we want to be able to translate our schemas into a W3C XML Schema.</para>
  426. <sect2>
  427. <title>Instance annotations</title>
  428. <para>What I'll be calling instance annotation in this book is the ability to attach to the instance document information gathered during the validation to facilitate its processing. Instance annotation is probably one of the most promising paths to automating XML document processing and its applications cover domains such as datatype assignment (which is one of the basis of XQuery 1.0, XPath 2.0 and XSLT 2.0), data binding (probably the only way to automate the creation of objects from XML documents and the creation of XML documents from objects) and XML guided editing.</para>
  429. <para>Some tools may have more stringent requirements depending on their algorithms (for instance, a SAX based streaming tool might want to impose deterministic schemas), but in theory (and in general), it is sufficient for the applications of instance annotations to insure that the annotations are consistent and this can be achieved if the schema is unambiguous.</para>
  430. <para>Note that even this condition isn't always required and that these requirements are application dependents. Consider for instance a databinding application which needs to know the content model of each element. This application might be in trouble to determine which content model to use if it finds a pattern such as:</para>
  431. <programlisting>
  432. <![CDATA[ element foo {first?,second}
  433. |element foo {second,third?}]]>
  434. </programlisting>
  435. <programlisting>
  436. <![CDATA[
  437. first=element first{xsd:integer}
  438. second=element second{xsd:token}
  439. third=element third{xsd:boolean}]]>
  440. </programlisting>
  441. <para>and an element foo with a content pattern matching the <literal>second</literal> pattern. Should it bind it into an object allowing an optional <literal>first</literal> or into an object allowing an optional <literal>third</literal>? Such ambiguity is likely to be an issue for this application. On the other hand, if all you need is to perform simple type assignment, this schema is perfectly fine since even though it is ambiguous, there is no ambiguity on datatype assignment.</para>
  442. <para>As a bottom line, we can say that chasing ambiguity in your Relax NG schemas may be considered a good practice if you have in mind instance annotation applications at large, you must also check the tools which you will be using since they can have either more stringent or more relaxed requirements.</para>
  443. </sect2>
  444. <sect2>
  445. <title>Compatibility with W3C XML Schema</title>
  446. <para>I have promised to give an example of unambiguous patterns which is not deterministic and can't be rewritten in a deterministic form and here it is! Let's consider a pattern describing a book as a sequence of odd and even pages:</para>
  447. <programlisting>
  448. <![CDATA[ <zeroOrMore>
  449. <ref name="odd"/>
  450. <ref name="even"/>
  451. </zeroOrMore>
  452. <optional>
  453. <ref name="odd"/>
  454. </optional>]]>
  455. </programlisting>
  456. <para>or:</para>
  457. <programlisting>
  458. <![CDATA[ (odd, even)*, odd?]]>
  459. </programlisting>
  460. <para>This pattern is not ambiguous since for any valid combinations of odd and even pages it is possible to know which pattern has matched each of the pages. It can't be deterministic since for each odd page, you need to look ahead at the next one to see if it is the last before knowing if an even page is required in next position.</para>
  461. <para>W3C XML Schema requires deterministic content models under the name of &quot;Unique Particle Attribution&quot; and &quot;Consistent Declaration&quot; rules and just can't describe such a simple and useful content model!</para>
  462. <para>Another example of non deterministic pattern is:</para>
  463. <programlisting>
  464. <![CDATA[ <choice>
  465. <element name="foo">
  466. <attribute name="bar"/>
  467. </element>
  468. <element name="foo">
  469. <element name="bar">
  470. <text/>
  471. </element>
  472. </element>
  473. </choice>]]>
  474. </programlisting>
  475. <para>or:</para>
  476. <programlisting>
  477. <![CDATA[ element foo {attribute bar} | element foo {element bar {text}}]]>
  478. </programlisting>
  479. <para>This one would seem easier to translate. At least, it can be factorized and rewritten as a deterministic pattern in Relax NG as:</para>
  480. <programlisting>
  481. <![CDATA[ <element name="foo">
  482. <choice>
  483. <attribute name="bar"/>
  484. <element name="bar">
  485. <text/>
  486. </element>
  487. </choice>
  488. </element>]]>
  489. </programlisting>
  490. <para>or:</para>
  491. <programlisting>
  492. <![CDATA[ element foo {attribute bar| element bar {text}}]]>
  493. </programlisting>
  494. <para>Unfortunately, this doesn't help to translate our schema into W3C XML Schema since this language doesn't know how to handle this type of situations mixing constraints on sub elements and attributes without using dark hacks with key definitions which don't work in all the cases.</para>
  495. <para>Making sure that your schemas are deterministic is thus a good practice when you plan to translate your schemas into W3C XML Schema schemas but unfortunately not a guarantee that they will translate gracefully. The only rule I can give if you want to make sure that your schemas will be easy to translate is to check the result of translation frequently as you write your schema and hope that James Clark will continue to improve the conversion algorithm!</para>
  496. <para>On the other hand, note that W3C XML Schema deals nicely with datatype ambiguities. If we take or example of datatype ambiguity:</para>
  497. <programlisting>
  498. <![CDATA[ element foo{xsd:boolean|xsd:integer}]]>
  499. </programlisting>
  500. <para>you will be surprised to know that it translates gracefully into:</para>
  501. <programlisting>
  502. <![CDATA[ <xs:element name="foo">
  503. <xs:simpleType>
  504. <xs:union memberTypes="xs:boolean xs:integer"/>
  505. </xs:simpleType>
  506. </xs:element>]]>
  507. </programlisting>
  508. <para>and that this is not considered as ambiguous by W3C XML Schema which has added a rule to say that when several datatypes where grouped &quot;by union&quot; which is basically what our choice between datatype does, a processor should stop after the first type which matches and not evaluate the next alternatives.</para>
  509. </sect2>
  510. </sect1>
  511. <sect1>
  512. <title>Some ideas to make disambiguation easier</title>
  513. <para>To close this chapter I'd like to present some ideas which would facilitate our lives in disambiguating schemas.</para>
  514. <sect2>
  515. <title>Generalized <literal>except</literal> pattern</title>
  516. <para>In the different forms of ambiguity, name classes has been the easiest one to disambiguate. Why is this? Not because name classes are inherently simpler than regular expressions or datatypes: all of them are about defining sets of things that can happen in a XML documents and I would argue that they are very similar. The reason why name classes have been easier to disambiguate is because they have a first class <literal>except</literal> operator and if we had the same level of support for patterns and datatypes, we could more easily disambiguate them.</para>
  517. <para>Applied to datatypes, this is already possible to some extend and away to disambiguate our example:</para>
  518. <programlisting>
  519. <![CDATA[ element foo{xsd:boolean|xsd:integer}]]>
  520. </programlisting>
  521. <para>is to write:</para>
  522. <programlisting>
  523. <![CDATA[ element foo{ (xsd:boolean - xsd:integer) |xsd:integer}]]>
  524. </programlisting>
  525. <para>A value which is only integer will obviously match only the right alternative. A value which is only boolean (i.e. <literal>true</literal> and <literal>false</literal>) will match the left alternative and a value which is both a boolean and an integer (i.e. <literal>0</literal> and <literal>1</literal>) will match the first condition of the left alternative (<literal>xsd:boolean</literal>) but will not match the exception clause.</para>
  526. <para>Unfortunately, this can't be generalized out of the scope of <literal>data</literal> patterns (note that the examples given below with the except (<literal>-</literal>) operator are not valid Relax NG).</para>
  527. <para>If this could be generalized, applied to an ambiguous regular expression such as:</para>
  528. <programlisting>
  529. <![CDATA[ two|(one?,two+,three*)]]>
  530. </programlisting>
  531. <para>We would be able to write:</para>
  532. <programlisting>
  533. <![CDATA[ two|((one?,two+,three*)-two)]]>
  534. </programlisting>
  535. <para>Of course, this can be developed and rewritten with the existing Relax NG patterns, but that would give a new level of flexibility to the language.</para>
  536. </sect2>
  537. <sect2>
  538. <title>Explicit disambiguation rules</title>
  539. <para>The second idea I'd like to give is far less disruptive and is just the realization that these ambiguities are just ambiguous because we have not decided anything to rule them out. There are plenty of examples in other computer languages of ambiguities which have been partially or fully ruled out such as for XSLT templates, order of evaluation of statements in programming languages or as we've seen in the section about W3C XML Schema union of datatypes.</para>
  540. <para>There is absolutely nothing preventing writing a specification defining a priority for the alternatives to be used by applications interested in instance annotation at large when they encounter ambiguities.</para>
  541. <para>This specification wouldn't need to apply to Relax NG processors interested only in validation and would not compromise their optimizations. It would only apply to NG processors performing instance annotation and guarantee a consistent and interoperable type annotation for schema which are today considered as ambiguous.</para>
  542. <para>The rule could be as simple as &quot;use the first alternative in document order&quot; or it could also take into account additional factors such as giving a lesser precedence to included grammars like XSLT does with stylesheet imports.</para>
  543. </sect2>
  544. <sect2>
  545. <title>Accepting ambiguity</title>
  546. <para>The third idea has been proposed by Jeni Tennison on the xml-dev mailing list: instead of trying to fight against ambiguity, why not accept it? Why couldn't we acknowledge that something can have several datatypes (or model) and have at the same type a datatype &quot;A&quot; and &quot;B&quot;? Why a value couldn't be at the same type an <literal>integer</literal> and a <literal>boolean</literal>?</para>
  547. <para>This idea would have a serious impact on specifications such as XPath 2.0 which assign a single datatype to each simple type element and attribute, but that would be much more compatible with the principle of markup which is only the projection of a structure over a document. It often happen that a piece of text may have several meaning, acknowledging that elements and attributes may have belong to datatypes at the same time just seems something obvious to do.</para>
  548. </sect2>
  549. </sect1>
  550. </chapter>