<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="file:///c:/sgml/public/styles/liteslides.msxsl"?>
<!DOCTYPE TEI.2 SYSTEM "C:\SGML\public\TEI\DERIVED\xmllite.dtd" [
<!ENTITY mdash "--">
]>
<TEI.2> 
<teiHeader> 
<fileDesc> 
<titleStmt> 
<title>Context-sensitive rules in XML Schema</title> 
</titleStmt> 
<publicationStmt> 
<publisher>World Wide Web Consortium</publisher> 
<pubPlace>Cambridge, Sophia-Antipolis, Tokyo</pubPlace> 
<date>2000</date> 
</publicationStmt> 
<sourceDesc> 
<p>Created in electronic form.</p>

</sourceDesc> 
</fileDesc>
<revisionDesc>
<list>
<item>2000-04-27 : CMSMcQ : add reference to Maler and el
Andaloussi</item>
<item>2000-04-26 : CMSMcQ : finish revision</item>
<item>2000-04-24 : CMSMcQ :work through, using Steve's comments.
Reorder sections and redo intro overview and transitions.</item>
<item>2000-03 : Steve DeRose : reads draft and comments</item>
<item>2000-02-15 : CMSMcQ : first draft begins as part of note to W3C
staff explaining the XML Schema WG's decision on issue 208; grows too
large, and becomes separate document</item>
</list>
</revisionDesc> 
</teiHeader> 
<text> 
<front> 
<titlePage> 
<docTitle> 
<titlePart>Context-sensitive rules in XML Schema</titlePart> 
</docTitle> 
<docAuthor>C. M. Sperberg-McQueen</docAuthor> 
<docDate>15 February 2000</docDate>
<docDate>rev. 25 April 2000</docDate> 
</titlePage> 
</front> 
<body> 
<p>The element declarations in SGML and XML document type definitions
(DTDs) specify the legal contents of an element type. The rules for
what can occur within an element are the same, no matter where it
occurs. 
<note place="foot">This is true of XML DTDs; it is slightly less true
of SGML DTDs, owing to the 
<term>inclusion and exclusion exceptions</term> constructs present in
SGML and omitted from XML. Inclusion and exclusion exceptions are an
attempt to deal with the problems I am about to describe. They were
omitted from XML because the complexity they introduce seems out of
proportion to the modest increase in expressive power they provide. As
the examples below should make clear to those familiar with exceptions
in SGML, the local definitions of XML Schema are a better approach and
can accomplish more. </note> Since the declaration of an element type
is thus independent of what, in formal terms, is called the 
<term>context</term> of the element, the DTD notation in XML 1.0 can be
said to define a form of 
<term>context-free</term> grammar. 
<note place="foot">The concepts of context-free and context-sensitive
grammars were first introduced, in their current form, by Chomsky 1957.
Detailed discussion can be found in virtually any textbook of parsing,
syntax-directed programs, and compiler construction. A good
introduction is in Grune.</note></p>

<p>It is fairly common in markup languages, however, to want slightly
different rules for an element depending on where it occurs in the
document. Consider, for example, the <gi>input</gi> element type in
HTML with forms. In principle, this should be able to occur anywhere
that text (character data) can occur, but only within a <gi>form</gi>
element. So, for example, it should be able to occur within a
paragraph, if the paragraph is within a <gi>form</gi> element, but not
otherwise. This is a simple example of context-sensitivity, and it has
historically been a source of astonishment to some users of SGML and
XML to learn that neither SGML nor XML DTDs are able to express it
formally.</p>

<p>This document describes how to use XML Schema to achieve some
context-sensitive effects not possible in XML or SGML document type
definitions (DTDs). The first section introduces a simple example of a
markup language in which some form of context-sensitivity is needed
&mdash; specifically, the rules governing the appearance of
<gi>input</gi> elements within HTML documents, and shows a simple DTD
for that markup language, as it might be written for SGML or XML
processors. The context-sensitive constraints cannot be expressed by
the formal element declarations, so they must be expressed separately,
in normative prose. The second section shows an equivalent XML Schema.
The third section uses some imaginary extensions to XML 1.0 DTD syntax
to show how the context-sensitive rules <emph>could</emph> be declared,
given a suitable notation. The fourth section shows how the XML Schema
language can be used to capture the context-sensitive constraints. 
<note place="foot">The author acknowledges his deep debt, and expresses
his deep thanks, to Murata Makoto for discussions of document grammars
over the last few years. The example outlined here using local types
was originally developed as an application of Murata's proposals to use
tree automata in defining document grammars. The complex types used
here serve much the same purpose as the non-terminals in Murata's
proposals.</note></p>

<p>This document assumes either a certain degree of familiarity with
XML, XML DTDs, HTML, and XML Schema, or a certain degree of willingness
to take the details on faith. 
<note place="foot">Since it would be desirable to make it accessible to
readers without such familiarity, anyone who is puzzled by some part of
the document is invited to contact the author and explain where you got
lost, and what information you think would have helped keep you from
getting lost.</note></p>

<div1>

<head>An example of context-sensitive rules</head>

<p>To illustrate the problem, let us consider a simple markup language
we'll call <ident>M</ident>. <ident>M</ident> is roughly similar to a
small subset of HTML with forms, though it has a slightly different
overall document structure.</p>

<p>The basic document structure of <ident>M</ident> is given by the
following element types: 
<list type="glossary">
<item><gi>html</gi> is the document element; each <gi>html</gi> element
contains a <gi>head</gi> and a <gi>body</gi>.</item>
<item><gi>head</gi> is a container for metadata pertaining to the
document.</item>
<item><gi>meta</gi> is an empty element specifying one piece of
metadata; the <ident>name</ident> attribute indicates what kind of
metadata, and the <ident>content</ident> attribute gives the
value.</item>
<item><gi>body</gi> contains the human-readable part of the document;
it consists of one or more <gi>form</gi>, <gi>div1</gi>, or <gi>p</gi>
elements.</item>
<item><gi>div1</gi> is a top-level text division (e.g. a chapter or a
section). Elements of type <gi>div1</gi> may occur only at the top
level; they cannot nest.</item>
<item><gi>h1</gi> is the title of a top-level text division.</item>
<item><gi>div2</gi> is a second-level text division (e.g. a
subsection). Elements of type <gi>div2</gi> may occur only within
<gi>div1</gi> elements; they cannot nest.</item>
<item><gi>h2</gi> is the title of a second-level text division.</item>
<item><gi>p</gi> is a paragraph.</item>
</list> </p>

<p>The <gi>form</gi> element in <ident>M</ident> is very similar to the
<gi>form</gi> element of HTML: 
<list type="glossary">
<item><gi>form</gi> is the container element for one interactive form;
when the user clicks on the appropriate submit button, the data will be
transmitted to the server identified by the <ident>action</ident>
attribute, using the method identified by the <ident>method</ident>
attribute.</item> 
<item><gi>item</gi> specifies one input field in the form; its
<ident>name</ident>, <ident>type</ident>, and <ident>value</ident>
attributes specify the field name, datatype, and default value
respectively.</item>
</list> The attributes specified for <gi>meta</gi>, <gi>form</gi>, and
<gi>item</gi> are given for verisimilitude; they don't actually affect
our example, though the methods described in this paper can also be
used to give limited context sensitivity to attribute definitions as
well as content models.</p>

<p><ident>M</ident> has a number of other rules about how these
elements interact: 
<list type="ordered"> 
<item> The hierarchy (<gi>body</gi> / <gi>div1</gi> / <gi>div2</gi> /
<gi>p</gi>) is unaffected by whether forms are present or not;
<gi>form</gi> is <soCalled>transparent</soCalled> to the
hierarchy.</item>
<item> Forms may appear at any level (directly within <gi>body</gi>,
<gi>div1</gi>, or <gi>div2</gi>) but may not appear within other
forms.</item>
<item>In <ident>M</ident>, as in HTML, the <gi>input</gi> element
should be able to occur anywhere that text (character data) can occur,
but only within a <gi>form</gi> element. So, for example, it should be
able to occur within a paragraph, if the paragraph is within a
<gi>form</gi> element, but not otherwise. </item> 
</list></p>

<p>An XML version of the sample DTD might look like this; we use
parameter entities in declaring some attributes, in order to stress the
nature of the expected value.<eg><![CDATA[
<!ELEMENT html    (head, body)                >
<!ELEMENT head    (meta*)                     >
<!ELEMENT meta    EMPTY                       >
<!ATTLIST meta
          name    NAME       #IMPLIED
          content CDATA      #REQUIRED        >
<!ELEMENT body    (form | div1 | p)+          >
<!ELEMENT div1    (h1, (form | div2 | p)+)    >
<!ELEMENT h1      (#PCDATA)                   >
<!ELEMENT div2    (h2,(form | p)+)            >
<!ELEMENT h2      (#PCDATA)                   >
<!ELEMENT p       (#PCDATA | input)*          >
<!ELEMENT input   EMPTY                       >
<!ENTITY  % WidgetType
          "(text | password | checkbox 
          | radio | submit | reset | file
          | hidden | image | button)"         >
<!ATTLIST input   
          name    NAME       #IMPLIED
          type    %WidgetType;  'text'
          value   CDATA      #IMPLIED         >
<!ELEMENT form    (p | div2 | div1)*          >
<!ENTITY  % URI   'CDATA'                     >
<!ATTLIST form
          action  %URI;      #REQUIRED
          method  (get|post) 'get'            >
]]></eg></p>

<p>Note that the <gi>form</gi> element can contain <gi>div1</gi> and
<gi>div2</gi> elements, and each of these can in turn contain a
<gi>form</gi> element. So a <gi>form</gi> element within a
<gi>div1</gi> element might also contain a <gi>div1</gi> element, thus
allowing <gi>div1</gi> elements to nest, thus violating rule 1. Even if
rule 1 is obeyed, a <gi>form</gi> element within a <gi>div1</gi>
element can contain a <gi>div2</gi> which in turn contains another
<gi>form</gi> element, thus violating rule 2. And there is no way to
specify that <gi>input</gi> elements may occur within paragraphs only
if a form is open, so rule 3 is not enforced by this DTD either. No XML
DTD can enforce any of the three rules just given, without outlawing
some documents which should be legal.</p>

<p>In SGML, rule 1 can be enforced 
<note place="foot">Well, mostly enforced. See if you can spot the gap
in coverage before the text mentions it.</note> by adding exclusion
exceptions to the declarations for <gi>div1</gi> and <gi>div2</gi> <eg><![CDATA[
<!ELEMENT div1  (h1, (form | div2 | p)+)
                                    -(div1) >
<!ELEMENT div2  (h2,(form | p)+)    -(div2) >
]]></eg> The exclusion exceptions mean that when the effective content
model of <gi>form</gi> varies with context. When a <gi>form</gi>
element occurs directly within the text <gi>body</gi>, its content
model is <code>(p | div2 | div1)*</code>. When it occurs directly
within a <gi>div1</gi>, it is not allowed to have any <gi>div1</gi>
children, so its effective content model is <code>(p | div2)*</code>.
When it occurs directly within a <gi>div2</gi>, it is not allowed to
have any <gi>div2</gi> children. And since <gi>div2</gi> elements can
occur only within <gi>div1</gi> elements, a <gi>form</gi> within a
<gi>div2</gi> cannot contain <gi>div1</gi> elements either, so its
effective content model is <code>(p)*</code>. Which is what we
want.</p>

<p>Or, almost what we want. Actually, a <gi>form</gi> which occurs
directly within a <gi>body</gi> element can contain a <gi>div2</gi>
element, thus allowing <gi>div2</gi> to occur outside of any
<gi>div1</gi>. So the exclusion exceptions have allowed us to enforce
some, but not all, of the consequences of rule 1.</p>

<p>Rule 2 can be enforced fully by SGML: adding an exclusion exception
to the declaration for <gi>form</gi>, we can prohibit self-nesting at
any depth.<eg><![CDATA[
<!ELEMENT form  (p | div2 | div1)*  -(form) >
]]></eg> The enforcement of prohibitions against self-nesting is
one of the things SGML exclusion exceptions do best.</p>

<p>The third rule requires a more complicated kind of context
sensitivity, and cannot be expressed in full in either SGML or XML.
SGML offers two mechanisms which can be used to enforce rule 3 in part,
and the HTML 2.0 and 3.2 DTDs each use one of them. First, we can
define <gi>input</gi> as an 
<term>inclusion exception</term> on the <gi>form</gi> element (as was
done in the DTD for HTML 2.0). <eg><![CDATA[
<!ELEMENT p     (#PCDATA)*                  >
<!ELEMENT input EMPTY                       >
<!ELEMENT form  (p | div2 | div1)*  -(form)
                                      +(input) >]]></eg>This ensures that <gi>input</gi> is
legal within <gi>form</gi> elements, and not otherwise. But it allows
<gi>input</gi> in locations where text is not allowed (e.g. between
paragraphs). So we must supply a rule, expressed in prose but not in
the DTD, that restricts <gi>input</gi> elements to appropriate
locations within the form.</p>

<p>The second method is to define <gi>input</gi> as a legal subelement
of <gi>p</gi> and other elements which can contain text, and supply a
rule, expressed in prose but not in the DTD, which says that
<gi>input</gi> is only legal if a <gi>form</gi> element is open. This
is what was done in HTML 3.2. <eg><![CDATA[
<!ELEMENT p     (#PCDATA | input)*          >
<!ELEMENT input EMPTY                       >
<!ELEMENT form  (p | div2 | div1)*  -(form) >]]></eg></p>

<p>In neither case can we formally define exactly the constraint we
want, with the result that SGML and XML systems guided by the DTD will
not enforce this rule; ad hoc software must be written to check to
ensure that <gi>input</gi> elements occur only where they are supposed
to.</p>

</div1>

<div1>

<head>An XML Schema without context sensitivity</head>

<p>A definition of <ident>M</ident> using XML Schema, which enforces
the context-sensitive rules, will be shown below. In order to introduce
the basic notations of XML Schema, however, let us begin with a direct
translation of this DTD into XML Schema, without any
context-sensitivity.</p>

<p>The <gi>html</gi> element contains a sequence consisting of a
<gi>head</gi> and a <gi>body</gi>:<eg><![CDATA[
  <xsd:element name="html"> 
    <xsd:complexType> 
      <xsd:sequence> 
        <xsd:element ref="head"/>
        <xsd:element ref="body"/>
      </xsd:sequence> 
    </xsd:complexType> 
  </xsd:element> 
]]></eg></p>

<p>The <gi>head</gi> element contains one or more <gi>meta</gi>
elements, which are empty, and carry <ident>name</ident> and
<ident>content</ident> attributes:<eg><![CDATA[
  <xsd:element name="head"> 
    <xsd:complexType> 
      <xsd:element ref="meta" 
                   minOccurs="0" 
                   maxOccurs="unbounded"/>
    </xsd:complexType> 
  </xsd:element> 

  <xsd:element name="meta"> 
    <xsd:complexType content="empty"> 
      <xsd:attribute name="name" type="xsd:Name" use="optional"> 
      </xsd:attribute> 
      <xsd:attribute name="content" type="xsd:string" use="required"> 
      </xsd:attribute> 
    </xsd:complexType> 
  </xsd:element> 
]]></eg></p>

<p>The body contains one or more <gi>form</gi>, <gi>div1</gi>, or
<gi>p</gi> elements, in any order. <eg><![CDATA[
  <xsd:element name="body"> 
    <xsd:complexType> 
      <xsd:choice minOccurs="1" 
                 maxOccurs="unbounded"> 
        <xsd:element ref="form"/>
        <xsd:element ref="div1"/>
        <xsd:element ref="p"/>
      </xsd:choice> 
    </xsd:complexType> 
  </xsd:element> 
]]></eg></p>

<p>The <gi>div1</gi> (first-level division) element contains first a
heading (<gi>h1</gi>) and then one or more form elements
(<gi>form</gi>), second-level divisions (<gi>div2</gi>), or paragraphs
(<gi>p</gi>).<eg><![CDATA[
   <xsd:element name="div1"> 
    <xsd:complexType> 
      <xsd:sequence> 
        <xsd:element ref="h1"/>
        <xsd:choice minOccurs="1" 
                    maxOccurs="unbounded"> 
          <xsd:element ref="form"/>
          <xsd:element ref="div2"/>
          <xsd:element ref="p"/>
        </xsd:choice> 
      </xsd:sequence> 
    </xsd:complexType> 
  </xsd:element> 
  
  <xsd:element name="h1"> 
    <xsd:simpleType base="string"> 
    </xsd:simpleType> 
  </xsd:element> 
]]></eg></p>

<p>Second-level divisions are analogous to first-level divisions, but
after their heading they can contain only forms and paragraphs:<eg><![CDATA[
  <xsd:element name="div2"> 
    <xsd:complexType> 
      <xsd:sequence> 
        <xsd:element ref="h2"/>
        <xsd:choice minOccurs="1" maxOccurs="unbounded"> 
          <xsd:element ref="form"/>
          <xsd:element ref="p"/>
        </xsd:choice> 
      </xsd:sequence> 
    </xsd:complexType> 
  </xsd:element> 
  
  <xsd:element name="h2"> 
    <xsd:simpleType base="string"> 
    </xsd:simpleType> 
  </xsd:element> 
]]></eg></p>

<p>Paragraphs can contain <gi>input</gi> elements mixed with character
data:<eg><![CDATA[
  <xsd:element name="p"> 
	 <xsd:complexType content="mixed"> 
		<xsd:element ref="input" 
                   minOccurs="0" 
                   maxOccurs="unbounded"/>
	 </xsd:complexType> 
  </xsd:element> 
]]></eg></p>

<p>And, finally, forms consist of paragraphs and divisions. Whether a
first- or second-level division is allowed within the form ought to
depend on whether the form occurs within any division itself -- but in
this formulation, it does not depend on that: either level is legal
inside the form, no matter what happened outside it.<eg><![CDATA[
  <xsd:element name="input"> 
    <xsd:complexType content="empty"> 
      <xsd:attribute name="name" 
                     type="xsd:Name" 
                     use="optional"/>
      <xsd:attribute name="type" 
                     type="WidgetType" 
                     use="required" 
                     value="text"/>
      <xsd:attribute name="value" 
                     type="xsd:string"/>
    </xsd:complexType> 
  </xsd:element> 
  <xsd:element name="form"> 
    <xsd:complexType> 
      <xsd:sequence minOccurs="0" maxOccurs="unbounded"> 
        <xsd:element ref="p"/>
        <xsd:element ref="div2"/>
        <xsd:element ref="div1"/>
      </xsd:sequence> 
      <xsd:attribute name="action" type="xsd:uriReference"/>
      <xsd:attribute name="method" type="method"/>
    </xsd:complexType> 
  </xsd:element>
]]></eg></p>

<p>The schema uses user-defined types, rather than parameter entities,
for specialized attribute datatypes like the set of legal widget types,
or the set of legal methods. We define those types by enumerating the
legal values:<eg><![CDATA[
  <xsd:simpleType base="urSimpleType" name="WidgetType"> 
    <xsd:enumeration value="text"/> 
    <xsd:enumeration value="password"/>     
    <xsd:enumeration value="checkbox"/> 
    <xsd:enumeration value="radio"/>     
    <xsd:enumeration value="submit"/>    
    <xsd:enumeration value="reset"/>    
    <xsd:enumeration value="file"/>     
    <xsd:enumeration value="hidden"/>     
    <xsd:enumeration value="image"/>     
    <xsd:enumeration value="button"/>    
  </xsd:simpleType>  
  <xsd:simpleType name="method" base="urSimpleType">
    <xsd:enumeration value="get"/>
    <xsd:enumeration value="post"/>
  </xsd:simpleType>
]]></eg>The <ident>uriReference</ident> type does not need to
be defined; it is a built-in type in XML Schema.</p>

</div1>

<div1>

<head>Adding context-sensitivity</head>

<p>The problem with the DTD and schema given above is that the content
models are fixed once and for all, and we would like them to vary
depending on the context of the element instances.</p>

<p>For example, the declaration for <gi>p</gi> should take one form if
the <gi>p</gi> is anywhere inside a <gi>form</gi> element:<eg><![CDATA[
<!ELEMENT p (#PCDATA | input)* >
]]></eg>but it
should take another form otherwise, without <gi>input</gi> elements:<eg><![CDATA[
<!ELEMENT p (#PCDATA) >
]]></eg></p>

<p>If we used parameter entities for the content models, as many DTDs
do, to give a name to each of these content models, the result might
look like this: <eg><![CDATA[
<!ENTITY % p-in-form "(#PCDATA | input)*" >
<!ENTITY % p-normal  "(#PCDATA)" >
]]></eg></p>

<p>Inside forms and outside them, <gi>p</gi> needs to have two distinct
content models:<eg><![CDATA[
<!--* inside forms:  *-->
<!ELEMENT p %p-in-form; >
<!--* outside of forms *-->
<!ELEMENT p %p-normal; >
]]></eg></p>

<p>Similarly <gi>form</gi> should allow a mix of <gi>div1</gi> and
<gi>p</gi> elements, if the form is outside of any <gi>div1</gi>
element:<eg><![CDATA[
<!ELEMENT form (div1 | p)* >
]]></eg>but inside a <gi>div1</gi> it should allow a
<gi>div2</gi>:<eg><![CDATA[
<!ELEMENT form (div2 | p)* >
]]></eg>and inside a <gi>div2</gi> it should allow no text
divisions at all, only paragraphs:<eg><![CDATA[
<!ELEMENT form (p)* >
]]></eg>Defined as parameter entities,
these three content models take the following form:<eg><![CDATA[
<!ENTITY % form-in-body "(div1 | p)*" >
<!ENTITY % form-in-div1 "(div2 | p)*" >
<!ENTITY % form-in-div2 "(p)*" >
]]></eg></p>

<p>Similar sets of varying declarations could be constructed for
<gi>div1</gi> and <gi>div2</gi>; this is left as an exercise for the
reader (hint: two of each).</p>

</div1>

<div1>

<head>An imaginary extension to DTD notation</head>

<p>In order to make clear just what is needed to formalize the
definition of our example markup language, let us use some imaginary
extensions to DTD notation. N.B. this is not a proposal for changing or
extending DTD notation, just an attempt to provide a more compact way
of seeing what is going on. Let us imagine that DTDs allow us to
declare element types in the familiar way, and to refer to them in
content models in the usual way. But let us imagine further that
whenever an element type is referred to in any content model, we can if
we wish specify a <emph>different</emph> content model for elements in
the document which match that particular content-model particle.
Formally, we can say that conventional element-type declarations bind a
generic identifier to a content model, globally, and that our new
syntax allows us to override the global binding with a 
<term>local</term> binding.</p>

<p>Concretely, 
<list> 
<item>In a content model, we allow a reference either to a generic
identifier (which identifies a top-level element) or to a
(GI:content-model) pair, which declares a local binding of that generic
identifier to that content model, for elements which match that
particular particle.</item> 
<item>In order to keep the (GI:content-model) pairs simple, we require
that such pairs refer to content models by name.</item>
<item>Allow a new <kw>TYPE</kw> declaration for content models, which
specifies a content-model name and a content model. (I am not making a
real proposal for reform of DTD notation, so I won't worry about simple
types or various other complications.)</item> 
</list></p>

<p>The parameter entities <ident>p-normal</ident>,
<ident>p-in-form</ident>, etc., which were defined above, now become
content-model names, and are declared with <kw>TYPE</kw> declarations. <eg><![CDATA[
<!ELEMENT html  (head, body)                >
<!ELEMENT head  (meta+)                     >
<!ELEMENT meta  EMPTY                       >
<!ELEMENT body  (form:form-in-body 
                | div1:div1-normal 
                | p:p-normal)+              >
<!ELEMENT h1    (#PCDATA)                   >
<!ELEMENT h2    (#PCDATA)                   >
<!ELEMENT input EMPTY                       >

<!TYPE p-normal (#PCDATA)                   >
<!TYPE p-in-form (#PCDATA | input)*         >

<!TYPE form-in-body 
                (div1:div1-in-form 
                | p:p-in-form )*            >
<!TYPE form-in-div1    
                (div2:div2-in-form
                | p:p-in-form)*             >
<!TYPE form-in-div2
                (p:p-in-form)*              >

<!TYPE div1-normal
                (h1, 
                (form:form-in-div1
                | div2:div2-normal 
                | p:p-normal)+)             >
<!TYPE div1-in-form   
                (h1,
                (div2:div2-in-form
                | p:p-in-form)+)            >

]]></eg></p>

<p>This imaginary extension of DTDs allows us to capture formally all
the rules of our example markup language <ident>M</ident>.</p>

</div1>

<div1>

<head>XML Schema with context-sensitivity</head>

<p> XML Schema can be used to allow declarations to vary by context, in
much the same way as the imaginary extensions to DTD notation just
described. The key is that in addition to declaring element types at
the top level, globally (i.e. as a global binding between an
element-type name and a complex type), we can declare element types 
<term>locally</term>, within the definition of a particular complex
type.</p>

<p>Parts of the schema (specifically the declarations for
<gi>html</gi>, <gi>head</gi>, <gi>meta</gi>, <gi>h1</gi>, <gi>h2</gi>,
and <gi>input</gi>, are the same as before.</p>

<p>For the other elements, first let us declare the various types we
need. Within these types we may either <emph>refer</emph> to 
<term>global</term> element types (by means of <code>&lt;element
ref="..."/&gt;</code>), or 
<term>define</term> new 
<term>local</term> element types (by means of <code>&lt;element
name="..." type="..."/&gt;)</code>. Where we need multiple forms of one
element type (or, to put it a different way, when we need the same
generic identifier to be bound to different complex types in different
contexts), we define named types for each variant. When we don't need
such complexity, we just define the element at the global level, as
already shown above.</p>

<p>Paragraphs can be of two types, which we name
<ident>p-normal</ident> and <ident>p-in-form</ident>: <eg><![CDATA[
  <xsd:complexType name="p-normal" content="textonly"/>
  <xsd:complexType name="p-in-form" content="mixed">
    <xsd:element ref="input" minOccurs="0" maxOccurs="unbounded"/>
  </xsd:complexType>
]]></eg></p>

<p>Forms can occur directly within the body of the text, or within
first- or second-level divisions; in each case, we need a distinct
complex type, which we name <ident>form-in-body</ident>,
<ident>form-in-div1</ident>, and <ident>form-in-div2</ident>. All three
of these have the same attributes, so we factor the attribute
declarations out into an attribute group: <eg><![CDATA[
  <xsd:attributeGroup name="form-atts">
    <xsd:attribute name="action" type="xsd:uriReference">
    </xsd:attribute>
    <xsd:attribute name="method" type="method">
    </xsd:attribute>
  </xsd:attributeGroup> 
  
  <xsd:complexType name="form-in-body">
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="div1" type="div1-in-form"/>
      <xsd:element name="p" type="p-in-form"/>
    </xsd:choice>
    <xsd:attributeGroup ref="form-atts"/>
  </xsd:complexType>

  <xsd:complexType name="form-in-div1">
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="div1" type="div1-in-form"/>
      <xsd:element name="p" type="p-in-form"/
    </xsd:choice>
    <xsd:attributeGroup ref="form-atts"/>
  </xsd:complexType>

  <xsd:complexType name="form-in-div2">
      <xsd:element name="p" 
                   type="p-in-form" 
                   minOccurs="0" 
                   maxOccurs="unbounded"/>
    <xsd:attributeGroup ref="form-atts"/>
  </xsd:complexType>
]]></eg></p>

<p>First-level divisions can occur directly within the body of the
text, or within a form. In the former case, they can contain forms or
second-level divisions, or paragraphs:<eg><![CDATA[
  <xsd:complexType name="div1-normal">
    <xsd:sequence>
      <xsd:element ref="h1"/>
      <xsd:choice minOccurs="1" maxOccurs="unbounded">
        <xsd:element name="form" type="form-in-div1"/>
        <xsd:element name="div2" type="div2-normal"/>
        <xsd:element name="p" type="p-normal"/>
      </xsd:choice>
    </xsd:sequence>
  </xsd:complexType>]]></eg>In the latter case, they
cannot contain nested forms, and they use the alternate inside-a-form
types for nested divisions and paragraphs:<eg><![CDATA[
  <xsd:complexType name="div1-in-form">
    <xsd:sequence>
      <xsd:element ref="h1"/>
      <xsd:choice minOccurs="1" maxOccurs="unbounded">
        <xsd:element name="div2" type="div2-in-form"/>
        <xsd:element name="p" type="p-in-form"/>
      <xsd:/choice>
    </xsd:sequence>
  </xsd:complexType>
]]></eg></p>

<p>Second-level divisions can occur directly within a first-level
division, or within a form which is itself inside a first-level
division. If no form is open, a form can occur within the
<gi>div2</gi>:<eg><![CDATA[
  <xsd:complexType name="div2-normal">
    <xsd:sequence>
      <xsd:element ref="h2"/>
      <xsd:choice minOccurs="1" maxOccurs="unbounded">
        <xsd:element name="form" type="form-in-div2"/>
        <xsd:element name="p" type="p-normal"/>
      </xsd:choice>
    </xsd:sequence>
  </xsd:complexType>
]]></eg>If a form is open outside the <gi>div2</gi>, no form
is allowed inside, only paragraphs:<eg><![CDATA[
  <xsd:complexType name="div2-in-form">
    <xsd:sequence>
      <xsd:element ref="h2"/>
      <xsd:element name="p" 
                   type="p-in-form" 
                   minOccurs="1" 
                   maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
]]></eg></p>

<p>We have now defined all the different complex types we need for the
context-sensitive elements. It remains only to declare the
<gi>body</gi> element, which specifies types
<ident>form-in-body</ident>, <ident>div1-normal</ident>, and
<ident>p-normal</ident> for its children:<eg><![CDATA[
  <xsd:element name="body">
    <xsd:complexType>
      <xsd:choice minoccurs="1" maxoccurs="unbounded">
        <xsd:element name="form" type="form-in-body"/>
        <xsd:element name="div1" type="div1-normal"/>
        <xsd:element name="p" type="p-normal"/>
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>
]]></eg></p>

<p>The new schema correctly enforces all the rules laid out at the
outset: 
<list> 
<item> Forms may appear at any level (<gi>body</gi>, <gi>div1</gi>,
<gi>div2</gi>) but may not appear within other forms. This is enforced
by the document grammar. All elements which can both appear within
forms and contain forms have two distinct types, one for each case, and
all descendants of a <gi>form</gi> element are associated with their
inside-a-form variant.</item> 
<item> The hierarchy (<gi>body</gi> / <gi>div1</gi> / <gi>div2</gi> /
<gi>p</gi>) is unaffected by whether forms are present or not;
<gi>form</gi> is <soCalled>transparent</soCalled> to the hierarchy.
(This is enforced by the context-dependent variations on <gi>form</gi>,
which allow within form only the element types which would have been
legal in the form's parent element).</item> 
<item>The input element is legal within a <gi>p</gi> if and only if the
<gi>p</gi> appears within an open form. This is enforced by the types
which contain paragraphs: if the type is of the inside-a-form variety,
<gi>p</gi> is bound to the appropriate complex type
<ident>p-in-form</ident>, and otherwise it is bound to the type
<ident>p-normal</ident>. </item> 
</list></p>

<p>Since these rules <emph>cannot</emph> be enforced by DTDs, it is
clear that local binding of GIs and types makes schemas strictly more
powerful (in this respect) than DTDs. Since such local bindings have
often been desired in document markup languages (see, for example,
Welty and Ide 1999), XML Schema should prove a better (more powerful
and more flexible) vehicle for defining such languages than DTDs have
been.</p>

</div1>

<div1>

<head>Capturing limited context sensitivity in context-free
grammars</head>

<p>We now have a fully worked out example of our basic problem. The
problem is simply described: we are writing a schema, which in essence
is a context-free grammar, and we are trying to use it to express
constraints on the data which depend on the context. The solution is
equally simply described: for each element whose content model should
vary by context, we write distinct forms of that content model, and
bind them to the appropriate generic identifier in the appropriate
locations. </p>

<p>This technique is an application of a a well-known technique for
capturing limited context-sensitivity in context-free grammars. This
section describes that basic technique, and describes a general method
of expressing context-sensitive constraints in document grammars which
allow local binding of generic identifiers to content models.</p>

<p>Let us consider a very simple example of context sensitivity:
subject-verb agreement in English. Consider the grammar <eg>
  S   ::= NP VP
  NP  ::= PN | N | Det N
  VP  ::= V | V NP
  PN  ::= Bill | Ed
  N   ::= students | teachers | student | teacher
  Det ::= a | some | the
  V   ::= like | likes | heckle | heckles | teaches | teach
</eg> This
grammar generates grammatical sentences like 
<q>the teacher teaches some students</q> and 
<q>Bill likes Ed</q>, but also non-grammatical sentences like 
<q>the teachers teaches a students</q>, etc. Number agreement between
subject and verb is -- in principle -- context-sensitive information: a
singular verb is allowed in the <ident>VP</ident> only in contexts
where the <ident>VP</ident> follows an <ident>NP</ident> containing a
singular noun. But a context-free grammar can capture this particular
bit of context-sensitive information, by distinguishing singular noun
phrases and verb phrases from their plural counterparts, thus: <eg>
  S   ::= NPs VPs | NPp VPp
  NP  ::= NPs | NPp
  NPs ::= PN | Ns | Ds Ns
  NPp ::= Np | Dp Np
  VPs ::= Vs | Vs NP
  VPp ::= Vp | Vp NP
  PN  ::= Bill | Ed
  Ns  ::= student | teacher
  Np  ::= students | teachers
  Ds  ::= a | some | the
  Dp  ::= some | the
  Vs  ::= likes | heckles | teaches
  Vp  ::= like | heckle | teach
</eg>
This grammar enforces subject-verb agreement, and thus enforces the
context-sensitive subject-verb agreement rule in a context-free
grammar. </p>

<p> Basically, we have doubled almost every production rule, to provide
a singular and a plural form. This technique trades size of the grammar
for power, and it generalizes: <emph>any</emph> finite number of pieces
of context-sensitive information can be carried around in a finite
context-free grammar (though not usually in a <emph>compact</emph>
context-free grammar). There are grammar systems which allow a more
compact form of specification, and which then generate the long
context-free grammar from the compact specifications.</p>

<p> To apply this technique to schemas, the procedure is as follows: 
<list> 
<item>Identify the context upon which some variation in the markup
language depends; let us call this the 
<term>top</term> element. For example, the language <ident>M</ident>
has the constraint that the <gi>p</gi> element should allow
<gi>input</gi> elements as children if the <gi>p</gi> is within a
<gi>form</gi> element, and otherwise not. The <gi>form</gi> element is
the top element. (For some constraints, there may be multiple
tops.)</item> 
<item>Identify the place where the constraint makes a difference. Call
this the 
<term>bottom</term> element. For example in the constraint just
described, <gi>p</gi> is the bottom element. (For some constraints,
there may be multiple bottoms.)</item> 
<item>Find every element type which can appear between a top and a
bottom. In our example, this set includes <gi>div1</gi> and
<gi>div2</gi>. Call these the 
<term>middle</term> elements. (In some cases, middle elements may also
be bottom elements.)</item> 
<item>The (generic identifier : type) pairings need to be changed
(doubled) for all bottom and middle elements. For each such element
type, there will be some simple or complex type <ident>T</ident>
associated with it. </item> 
<item> For each type <ident>T</ident> associated with a bottom element,
write a new type <ident>T'</ident> which enforces the context-sensitive
constraint (or allows the context-dependent freedom). In our example,
we will write a version of the complex type for <gi>p</gi> which allows
<gi>input</gi> elements.</item> 
<item>For each type <ident>T</ident> associated with a middle element,
write a new type <ident>T'</ident>; call this the 
<term>context-dependent</term> version of <ident>T</ident>.
<ident>T'</ident> is just like <ident>T</ident> except that: 
<list> 
<item>If <ident>T</ident> allows any middle element type
<ident>E</ident> as a child, then in <ident>T'</ident> the reference to
the global element type <ident>E</ident> is replaced with a local
element declaration. The local element declaration will use the same
generic identifier, but the type <ident>U</ident> associated with
<ident>E</ident> will be replaced by <ident>U'</ident>, the
context-dependent version of <ident>U</ident>. </item> 
</list></item> 
<item>Finally, replace the original form of the type for the top
element with a context-dependent version of the same type.</item> 
</list> </p>

</div1>

</body> 
<back> 
<div1>

<head>References</head>

<listBibl>
<bibl>Chomsky, Noam. 
<title level="a">On certain formal properties of grammars</title>. 
<title level="j">Information and Control</title> 2 (1959):
137-167.</bibl>
<bibl>DeRose, Steven J. 
<title>The SGML FAQ Book: Understanding the Foundation of HTML and
XML</title>. Boston, Dordrecht, London: Kluwer Academic, 1997.
Questions 5.5 (pp. 105-106) and 6.1 (pp. 139-142) discuss content-model
exceptions and touch on issues of context sensitivity.</bibl> 
<bibl>Grune, Dick, and Ceriel J.H. Jacobs. 
<title>Parsing Techniques: A Practical Guide</title>. New York, London:
Ellis Horwood, 1990.</bibl>
<bibl>Maler, Eve, and Jeanne el Andaloussi. 
<title>Developing SGML DTDs: From Text to Model to Markup</title>.
Upper Saddle River, NJ: Prentice Hall PTR, 1996. Section 6.1.3
discusses the possibility of merging distinct content models, and
observes that the choice among them may be determined by context, but
the correct choice cannot be checked by the DTD.</bibl>
<!--<bibl>Megginson?</bibl>
<bibl>Jelliffe?</bibl>-->
<bibl>Berners-Lee, Tim, and Dan Connolly. 
<title>Hypertext Markup Language - 2.0</title>. RFC 1866. [n.p.]:
Internet Engineering Task Force, November 1995.
<xref>ftp://ftp.isi.edu/in-notes/rfc1866.txt</xref></bibl>
<bibl>Raggett, Dave. 
<title>HTML 3.2 Reference Specification</title>. W3C Recommendation
14-Jan-1997. [Cambridge, Sophia-Antipolis, Tokyo]: W3C, 1997.
<xref>http://www.w3.org/TR/REC-html32</xref></bibl>
<bibl> Welty, Christopher, and Nancy Ide. 
<title level="a">Using the Right Tools: Enhancing Retrieval from
Marked-up Documents.</title> 
<title>CHum</title> 33 (1999): 59-84. Originally delivered at TEI 10,
Providence (1997). </bibl>
</listBibl>
</div1>

</back> 
</text> 
</TEI.2>
