{"id":698,"date":"2009-01-14T20:33:35","date_gmt":"2009-01-15T03:33:35","guid":{"rendered":"http:\/\/semanticbible.com\/blogos\/2009\/01\/14\/xml-schema-with-optional-unbounded-unordered-elements\/"},"modified":"2009-01-14T20:33:35","modified_gmt":"2009-01-15T03:33:35","slug":"xml-schema-with-optional-unbounded-unordered-elements","status":"publish","type":"post","link":"https:\/\/semanticbible.com\/blogos\/2009\/01\/14\/xml-schema-with-optional-unbounded-unordered-elements\/","title":{"rendered":"XML Schema with Optional, Unbounded, Unordered Elements"},"content":{"rendered":"<p>This is so obscure i hesitate to blog about it, except that it took me so long to figure out that i&#8217;d love to save somebody else the trouble. You won&#8217;t care unless:<\/p>\n<ul>\n<li>You&#8217;re designing an XML Schema definition (.xsd) to validate an XML file<\/li>\n<li>You&#8217;re defining an element to contain regular text, or multiple elements, in any order, from zero to many times<\/li>\n<\/ul>\n<p>Here&#8217;s an example: suppose you have a plain text description of events that includes people, places, and Bible references.<\/p>\n<blockquote><p>Jesus heals Simon&#8217;s mother-in-law (<cite class=\"bibleref\">Matt 8:14-17<\/cite>; <cite class=\"bibleref\">Mark 1:29-34<\/cite>; <cite class=\"bibleref\">Luke 4:38-41<\/cite>)<\/p><\/blockquote>\n<p>You want to link person references with a Link element, Bible references with a Reference element, and otherwise leave the plain text as is. This results in something like this (using square brackets since otherwise WordPress gets confused):<\/p>\n<blockquote><p>[Link]Jesus[\/Link] heals [Link]Simon[\/Link]&#8217;s mother-in-law ([Reference]<cite class=\"bibleref\">Matt 8:14-17<\/cite>[\/Reference]; [Reference]<cite class=\"bibleref\">Mark 1:29-34<\/cite>[\/Reference]; [Reference]<cite class=\"bibleref\">Luke 4:38-41<\/cite>[\/Reference])<\/p><\/blockquote>\n<p>Now imagine several of these in the same element, so potentially you can have any arbitrary sequence of Links, References, and plain text, in any order, any number of times. Describing this with <a title=\"Wikipedia: Backus-Naur Form\" href=\"http:\/\/en.wikipedia.org\/wiki\/Backus%E2%80%93Naur_Form\">a BNF grammar<\/a> is trivial:<\/p>\n<blockquote><p><code> LinkRef ::= Link | Reference<br \/>\nTextItem ::=\u00c2\u00a0 ( text | LinkRef )+ <\/code><\/p><\/blockquote>\n<p>A cursory reading of the XML Schema description (which i&#8217;d never actually done before, instead depending on <a title=\"XMLSpy product page\" href=\"http:\/\/www.altova.com\/products\/xmlspy\/xml_editor.html\">XMLSpy<\/a> which generally lets me avoid thinking that hard) might make you think grouping models like <code>sequence<\/code>, <code>choice<\/code>, and <code>all<\/code> in conjunction with attributes like <code>minOccurs<\/code> and <code>maxOccurs<\/code> would do what you need. But there&#8217;s a surprisingly complex set of interactions between these, that i still don&#8217;t really understand, and so what seemed so simple proved surprisingly hard. Here are a few examples of what i tried, where XMLSpy&#8217;s validation model for XSD files (which i&#8217;m assuming is correct) wouldn&#8217;t allow it:<\/p>\n<ul>\n<li>while <code>all<\/code> is for an unordered group of elements, it&#8217;s restricted to <code>maxOccurs<\/code>=1. So it doesn&#8217;t handle unbounded occurrence (though it does allow <code>minOccurs<\/code>=0, e.g. optionality). Furthermore, it can&#8217;t be nested inside other model groups like <code>sequence<\/code>.<\/li>\n<li><code>choice<\/code> groupings can be neither optional nor unbounded.<\/li>\n<li>trying to specify multiple occurrences of both Link and Reference, each both optional and unbounded, is flagged as an ambiguous model.<\/li>\n<\/ul>\n<p>The solution i finally discovered (after embarrassingly many other permutations, more by trial and error than anything else):<\/p>\n<ul>\n<li>define a LinkRef <code>group<\/code> that allows a <code>sequence<\/code> of either Link or Reference, both optional and unbounded (zero to many occurrences)<\/li>\n<li>the TextItem (enclosing parent)  element allows an optional and unbounded <code>sequence<\/code> of LinkRef <code>group<\/code>s.<\/li>\n<\/ul>\n<p>For the more visually oriented, here&#8217;s how it looks in XMLSpy:<\/p>\n<p><img alt=\"TextItem and LinkRef Grouping\" id=\"image715\" src=\"http:\/\/semanticbible.com\/blogos\/wp-content\/uploads\/TextItem-LinkRef-grouping.png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is so obscure i hesitate to blog about it, except that it took me so long to figure out that i&#8217;d love to save somebody else the trouble. You won&#8217;t care unless: You&#8217;re designing an XML Schema definition (.xsd) to validate an XML file You&#8217;re defining an element to contain regular text, or multiple &hellip; <a href=\"https:\/\/semanticbible.com\/blogos\/2009\/01\/14\/xml-schema-with-optional-unbounded-unordered-elements\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">XML Schema with Optional, Unbounded, Unordered Elements<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/posts\/698"}],"collection":[{"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/comments?post=698"}],"version-history":[{"count":0,"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/posts\/698\/revisions"}],"wp:attachment":[{"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/media?parent=698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/categories?post=698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/semanticbible.com\/blogos\/wp-json\/wp\/v2\/tags?post=698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}