Extending MathML, continued

[28 April 2014]

In an earlier post, I talked about how to extend the MathML schema by using element substitution groups. I tend to think of that as the best way, other things being equal, to extend a schema.

But it’s also possible to extend the MathML schema by using xsd:redefine; this post explains what’s involved.

Step by step

Define the extension elements

First, we make a schema document for our own namespace, including the extension elements. We can use the first version of the schema document given in the earlier post, before we messed around with the type definitions for the new elements.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:my="http://example.com/nss/sample"
  targetNamespace="http://example.com/nss/sample"
  elementFormDefault="qualified"> 

  <xs:complexType name="extension-type-1" mixed="true">
    <xs:sequence/>
    &t;xs:attribute name="id" type="xs:ID"/>
    <xs:attribute name="flavor"/>
    <xs:attribute name="tone"/>
  </xs:complexType>
  <xs:complexType name="extension-type-2" mixed="true">
    <xs:sequence/>
    <xs:attribute name="gloss" type="xs:IDREF" use="required"/>
  </xs:complexType>  

  <xs:element name="ext1" type="my:extension-type"/>
  <xs:element name="ext2" type="my:extension-type"/>

</xs:schema>

Make a new MathML schema document

Next, we make a new top-level schema document to use when we import the MathML namespace. The new document will use xsd:redefine to point to the standard top-level or root schema document for MathML, and specify some changes.

In particular, we decide that we want to add our extension elements to the content-model group named mathml:Presentation-layout.class. We’ll define a group with that name, with the slightly unusual property that our new group will include a recursive reference to the existing group of that name, as a child. Such recursion is normally forbidden in content-model groups, but when redefining them, it’s obligatory. The group will look like this:

<xs:group name="Presentation-layout.class">
  <xs:choice>
    <xs:element ref="my:ext1"/>
    <xs:element ref="my:ext2"/>
    <xs:group ref="mathml:Presentation-layout.class"/>
  </xs:choice>
</xs:group>

The new top-level schema document for MathML will wrap that group definition in an xsd:redefine element, and will contain nothing else.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mathml="http://www.w3.org/1998/Math/MathML"
  xmlns:my="http://example.com/nss/sample"
  targetNamespace="http://www.w3.org/1998/Math/MathML"
  elementFormDefault="qualified">
  
  <xs:import namespace="http://example.com/nss/sample"/>

  <xs:redefine schemaLocation="../standard-modules/mathml2/mathml2.xsd">
    <xs:annotation>
      <xs:documentation> This is a modified copy of the MathML 2
        schema documents at http://www.w3.org/Math/XMLSchema/mathml2.tgz. 

        We use xsd:redefine to extend the Presentation-layout class to
        include two extension elements. </xs:documentation>
    </xs:annotation>

    <xs:group name="Presentation-layout.class">
      <xs:choice>
        <xs:element ref="my:ext1"/>
        <xs:element ref="my:ext2"/>
        <xs:group ref="mathml:Presentation-layout.class"/>
      </xs:choice>
    </xs:group>
  </xs:redefine>

</xs:schema>

Point to the new MathML schema document, not the old

Finally, we make the top-level driver document for our schema import our redefinition of MathML, not the standard MathML schema documents. In a normal schema importing the MathML namespace, we might have something like this:

<xsd:import namespace="http://www.w3.org/1998/Math/MathML"
            schemaLocation="standard-modules/mathml2/mathml2.xsd"/>

In our modified schema we have:

<xsd:import namespace="http://www.w3.org/1998/Math/MathML"
            schemaLocation="local-mods/my-modified-mathml2.xsd"/>

Advantages and disadvantages

Using xsd:redefine has one advantage over using substitution groups: we didn’t have to muck with the type definitions of our extension elements in order to derive those types from the type used by the substitution-group head. It has a few disadvantages worth mentioning.

First, every group and type we redefine is required to refer to the group or type being redefined. So we cannot use xsd:redefine to make arbitrary changes in a group or type. This was not a problem in our example, but it can be just as constraining, in its way, as the type-derivation requirement on substitution groups.

Second, despite the restrictions on what can be done in a redefinition, xsd:redefine does not guarantee that the relation of our extended schema to the base schema is easy to explain, document, or understand. Documents valid against the base schema are not guaranteed to be valid against the extended schema, so we don’t necessarily have a clean extension. Nor is there a convenient way to ensure that documents valid against the redefinition are always valid against the base schema, in cases where we want a clean restriction. The redefined groups, or types, may turn out to violate some other constraint on schemas, so there is absolutely no guarantee that a redefinition which conforms to all the constraints in the spec, applied to a schema which conforms to all the constraints in the spec, will produce a resulting schema which conforms to the constraints in the spec. Under these circumstances, it’s not surprising that some informed observers regard the constraints on xsd:redefine as pointless.

And, third, both the XSD 1.0 and the XSD 1.1 specs are internally contradictory with regard to xsd:redefine, and it is not hard to find sets of schema documents which behave very differently in different implementations as a result of the implementors having different interpretations of the spec. It is possible to use xsd:redefine in ways that will be consistently implemented by different XSD validators, but it’s very easy to find yourself locked in to a particular validator if you’re not careful.

Careful readers will have noticed that some imports in the schema documents shown have schemaLocation attributes, and some don’t. The simplest way to achieve interoperability is to keep things very very simple, and never ever tell a validator more than once where to find a schema document for a given namespace. My way of doing that is always to have a top-level document for the schema that performs all the imports and includes needed for the schema, and provides explicit schema location information, and to insist as a general rule that no other schema documents ever contain schema location information, unless (as in the case of an xsd:redefine) it is required for conformance to the spec. That way, the schema validator never sees two different schema locations for the same namespace, and we never need to worry about the fact that in such a situation different implementations of XSD may make different choices about which schema location to load and which to ignore. It is especially important to avoid the situation of having one schema document in your input import a particular namespace, while another redefines that same namespace: when that happens, no two XSD processors behave the same way. (This may seem implausible, since there are surely more than two XSD processors in the world. But it turns out that there are more than two different behaviors possible in the situation described. I once tested seven validators on an example, with different ways of formulating the command line, and got nine different behaviors from the set of seven processors.)

Extending MathML 2

[24 April 2014]

The other day I got an inquiry from a user having trouble getting their extensions to MathML 2 to work in their new XSD schema. I learned some things while working on their problem.

First, let’s be clear. MathML says that it is intended to be extensible. Section 7.3.2 of MathML2 reads in full:

The set of elements and attributes specified in the MathML specification are necessary for rendering common mathematical expressions. It is recognized that not all mathematical notation is covered by this set of elements, that new notations are continually invented, and that sub-communities within mathematics often have specialized notations; and furthermore that the explicit extension of a standard is a necessarily slow and conservative process. This implies that the MathML standard could never explicitly cover all the presentational forms used by every sub-community of authors and readers of mathematics, much less encode all mathematical content.

In order to facilitate the use of MathML by the widest possible audience, and to enable its smooth evolution to encompass more notational forms and more mathematical content (perhaps eventually covered by explicit extensions to the standard), the set of tags and attributes is open-ended, in the sense described in this section.

MathML is described by an XML DTD, which necessarily limits the elements and attributes to those occurring in the DTD. Renderers desiring to accept non-standard elements or attributes, and authors desiring to include these in documents, should accept or produce documents that conform to an appropriately extended XML DTD that has the standard MathML DTD as a subset.

MathML renderers are allowed, but not required, to accept non-standard elements and attributes, and to render them in any way. If a renderer does not accept some or all non-standard tags, it is encouraged either to handle them as errors as described above for elements with the wrong number of arguments, or to render their arguments as if they were arguments to an mrow, in either case rendering all standard parts of the input in the normal way.

I don’t find this passage in MathML3, but the sample embedding of MathML into XHTML does extend the document grammar to include XHTML elements, so I believe that the design principle remains true.

It’s easy enough to extend the document grammar as expressed by the DTD: just provide new declarations of appropriate parameter entity references which include your new elements, something
along the following lines. Let us say that we have concluded that we want our extension elements to be legal everywhere that mml:mspace is legal, and we don’t need them anywhere else. We can write:

<!ENTITY % my.mml.extensions "my:ext1 | my:ext2">
<!ENTITY % petoken "%mspace.qname; | %my.mml.extensions;" >
<!ELEMENT my:ext1 (#PCDATA) >
<!ATTLIST my:ext1 
     id ID #IMPLIED 
     flavor CDATA #IMPLIED 
     tone CDATA #IMPLIED >
<!ELEMENT my:ext2 EMPTY >
<!ATTLIST my:ext2 
     gloss IDEREF #REQUIRED >

For XSD, it could in principle be even simpler. The simplest way to make an XSD schema easily extensible is to include wildcards at appropriate points in content models, to allow users’ extension elements to be included in valid documents. All the user has to do is supply a schema document with the declarations of their extension elements:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:my="http://example.com/nss/sample"
  targetNamespace="http://example.com/nss/sample"
  elementFormDefault="qualified"> 

  <xs:complexType name="extension-type-1" mixed="true">
    <xs:sequence/>
    <xs:attribute name="id" type="xs:ID"/>
    <xs:attribute name="flavor"/>
    <xs:attribute name="tone"/>
  </xs:complexType>
  <xs:complexType name="extension-type-2" mixed="true">
    <xs:sequence/>
    <xs:attribute name="gloss" type="xs:IDREF" use="required"/>
  </xs:complexType>  

  <xs:element name="ext1" type="my:extension-type"/>
  <xs:element name="ext2" type="my:extension-type"/>

</xs:schema>

In the MathML 2 XSD, it turns out to be slightly more complicated, because despite explicitly expecting extensions to the document grammar, the designers didn’t put in the most obvious possible extensibility hook: the content models of MathML elements contain no wildcards, except in the case of the annotation element. So we have some more work to do.

Plan B is to use element substitution groups. Since we want our elements to be legal wherever mml:mspace is legal, we can just make our elements substitutable for mml:mspace. In the simplest case, we would then just write our schema document thus:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:my="http://example.com/nss/sample"
  xmlns:mml="http://www.w3.org/1998/Math/MathML"
  targetNamespace="http://example.com/nss/sample"
  elementFormDefault="qualified"> 

  <xs:import namespace="http://www.w3.org/1998/Math/MathML"/>
  
  <xs:element name="ext1" substitutionGroup="mml:mspace"/>
  <xs:element name="ext2" substitutionGroup="mml:mspace"/>

</xs:schema>

The wrinkle here is that when we write it this way, our extension elements get the same type as their substitution-group head, mml:mspace. If we just reinsert the declarations of my:extension-type-1 and my:extension-type-2 and the type attributes on the element declarations, the XSD validator will remind us firmly but politely (in most cases) that the types of my:ext1 and my:ext2 must be derived from that of mml:mspace. In the case of the XSD schema for MathML 2, that means they must be derived from type mml:mspace.type. For document-oriented schemas, this type-derivation requirement is a nuisance; it came from the data-base-oriented part of the working group that specified XSD.

Fortunately, it’s only a nuisance, not a serious obstacle. All we need to do is to define our extension types in terms of changes to type mml:mspace.type. This will require a couple of intermediate types which we’ll call bridge types. The first step in the derivation is to clear away everything we don’t want in our extension types, by restricting away any unwanted content (we’re in luck: mml:mspace.type has no content at all) and any unwanted attribute (again we’re in luck: all attributes are optional). Since one of our extension types uses an id attribute and the other does not, we’ll define two bridge types.

   <xsd:complexType name="bridge-with-id">
     <xsd:complexContent>
       <xsd:restriction base="mml:mspace.type">
         <xsd:attribute name="width" use="prohibited"/>
         <xsd:attribute name="height" use="prohibited"/>
         <xsd:attribute name="depth" use="prohibited"/>
         <xsd:attribute name="linebreak" use="prohibited"/>
         <xsd:attribute name="class" use="prohibited"/>
         <xsd:attribute name="style" use="prohibited"/>
         <xsd:attribute name="xref" use="prohibited"/>
         <xsd:attribute ref="xlink:href" use="prohibited"/>
       </xsd:restriction>
     </xsd:complexContent>
   </xsd:complexType>
  
  <xsd:complexType name="bridge-no-id">
     <xsd:complexContent>
       <xsd:restriction base="my:bridge-with-id">
         <xsd:attribute name="id" use="prohibited"/>
       </xsd:restriction>
     </xsd:complexContent>
   </xsd:complexType>

Now we can define our extension types in terms of these (perhaps biting our tongues at the verbosity and awkwardness of the XSD syntax):

  <xs:complexType name="extension-type-1" mixed="true">
    <xs:complexContent>
      <xs:extension base="my:bridge-with-id">        
        <xs:sequence/>
        <xs:attribute name="id" type="xs:ID"/>
        <xs:attribute name="flavor"/>
        <xs:attribute name="tone"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="extension-type-2" mixed="true">
    <xs:complexContent>
      <xs:extension base="my:bridge-no-id">        
        <xs:sequence/>
        <xs:attribute name="gloss" type="xs:IDREF" use="required"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

The reference to xlink:href requires that we import the XLink namespace (even though all we’re doing is saying we don’t want that attribute here), so we need to add another xs:import element as well as another namespace declaration.

The schema document as a whole now looks like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:my="http://example.com/nss/sample"
  xmlns:mml="http://www.w3.org/1998/Math/MathML"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  targetNamespace="http://example.com/nss/sample"
  elementFormDefault="qualified"> 
  
  <xs:import namespace="http://www.w3.org/1998/Math/MathML"/>  
  <xs:import namespace="http://www.w3.org/1999/xlink"/>
  
  <xs:complexType name="bridge-with-id">
    <xs:complexContent>
      <xs:restriction base="mml:mspace.type">
        <xs:attribute name="width" use="prohibited"/>
        <xs:attribute name="height" use="prohibited"/>
        <xs:attribute name="depth" use="prohibited"/>
        <xs:attribute name="linebreak" use="prohibited"/>
        <xs:attribute name="class" use="prohibited"/>
        <xs:attribute name="style" use="prohibited"/>
        <xs:attribute name="xref" use="prohibited"/>
        <xs:attribute ref="xlink:href" use="prohibited"/>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
  
  <xs:complexType name="bridge-no-id">
    <xs:complexContent>
      <xs:restriction base="my:bridge-with-id">
        <xs:attribute name="id" use="prohibited"/>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
  
  <xs:complexType name="extension-type-1" mixed="true">
    <xs:complexContent>
      <xs:extension base="my:bridge-with-id">        
        <xs:sequence/>
        <xs:attribute name="id" type="xs:ID"/>
        <xs:attribute name="flavor"/>
        <xs:attribute name="tone"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="extension-type-2" mixed="true">
    <xs:complexContent>
      <xs:extension base="my:bridge-no-id">        
        <xs:sequence/>
        <xs:attribute name="gloss" type="xs:IDREF" use="required"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  
  <xs:element name="ext1" type="my:extension-type-1"/>
  <xs:element name="ext2" type="my:extension-type-2"/>

</xs:schema>

There are two easy ways a vocabulary designer can make this process simpler:

  • Include wildcards at points where you want your grammar to be extensible.

This is a bit of a blunt instrument, but it sometimes gets the job done.

If you want to give the extender more control (perhaps they want some extension elements to be legal in some contexts and others to be legal in other contexts), give them extension hooks in the form of abstract elements with a minimally constraining type (e.g. xs:anyType), so that they don’t need to play games with type derivations, the way it was necessary to do for the MathML 2 extension.

  • Include abstract elements with minimally constraining types at points where you want your grammar to be extensible in context-appropriate ways.

As a general rule: any important element class in your document grammar (e.g. phrase-level-element or list or paragraph-level-element) is a good candidate for an abstract element intended to allow users to add new elements simply by making their new elements substitutable for the appropriate abstract element. (We have a new phrase-level element? Fine: declare <xs:element name="new-phrase" substitutionGroup="target:phrase-level-element"/> and we’re done.)

Of course, the determinism rules (aka Unique Particle Attribution constraint) in XSD still make extending a complex document grammar harder than it needs to be. But by providing appropriate extension hooks, the designer of a document grammar can make things a lot simpler for the user with special needs.