XML technologies / XML Schema / Documenting schemas / The annotation element of W3C XML Schema

(Excerpt from “XML Schema” by Eric van der Vlist)

The recommended way to add comments and documentation to a W3C XML Schema uses annotation element xs:annotation. It can be added to almost every element of W3C XML Schema, except xs:annotation itself and its children, xs:documentation and xs:appinfo. It normally comes first, except in element xs:schema, where xs:annotation may occur anywhere.

Element xs:annotation is a container for elements xs:documentation and xs:appinfo, holding extra information: human-readable documentation (xs:documentation) or machine-processable information (xs:appinfo). Both accept any text and child elements, the only W3C XML Schema elements with mixed content. The schema for schemas specifies lax content processing. Elements of W3C XML Schema can thus occur here but must be valid under the schema for schemas. Mixed content permits almost anything, including text:

<xs:element name="author" type="authorType">
   <xs:annotation>
      <xs:documentation xml:lang="en">The author of a book.</xs:documentation>
      <xs:documentation xml:lang="de">Der Autor eines Buches.</xs:documentation>
      <xs:documentation xml:lang="fr">Designe l'auteur d'un livre.</xs:documentation>
   </xs:annotation>
</xs:element>

Rich content such as XHTML can also be assembled for more readable, user-friendly documentation:

<xs:element name="author" type="authorType">
   <xs:annotation>
      <xs:documentation xml:lang="de">
         <p id="author" xmlns="http://www.w3.org/1999/xhtml">Dieses Element beschreibt den <em>Autor</em> eines <a href="#book">Buches</a>.</p>
      </xs:documentation>
   </xs:annotation>
</xs:element>  

It even accepts SVG, as this depiction of an author illustrates:

<xs:element name="author" type="authorType">
   <xs:annotation>
      <xs:documentation>
         <svg xmlns="http://www.w3.org/2000/svg">
            <title>Ein Autor</title>
            <ellipse style="stroke:#000000;fill:#e3e000;stroke-width:2pt;" id="head" cx="280" cy="250" rx="110" ry="130"/>
            <ellipse style="stroke:none;fill:#7f7f7f;" id="leftEye" cx="240"cy="225" rx="18" ry="18"/>
            <ellipse style="stroke:none;fill:#7f7f7f;" id="rightEye" cx="320"cy="225" rx="18" ry="18"/>
            <path style="fill:none;stroke:#7F7F7F;stroke-width:5pt;" id="mouth"d="M 222 280 A 58 48 0 0 0 338 280"/>
         </svg>
      </xs:documentation>
   </xs:annotation>
</xs:element>

Dublin Core elements characterize webpages, are widely supported, and can be embedded as general metadata in annotations:

<xs:element name="author" type="authorType">
   <xs:annotation>
      <xs:appinfo xmlns:dc="http://purl.org/dc/elements/1.1/">
         <dc:creator>Eric van der Vlist (mailto:vdv@dyomedea.com)</dc:creator>
         <dc:date>2002-02-01</dc:date>
         <dc:subject>Autor,Person,Buch</dc:subject>
         <dc:description>Dieses Element beschreibt den Autor eines Buches.</dc:description>
      </xs:appinfo>
   </xs:annotation>
</xs:element>

Annotations also hold application-specific metadata. The schema for schemas uses it to describe facets and properties of primitive data types:

<xs:simpleType name="string" id="string">
   <xs:annotation>
      <xs:appinfo>
         <hfp:hasFacet name="length"/>
         <hfp:hasFacet name="minLength"/>
         <hfp:hasFacet name="maxLength"/>
         <hfp:hasFacet name="pattern"/>
         <hfp:hasFacet name="enumeration"/>
         <hfp:hasFacet name="whiteSpace"/>
         <hfp:hasProperty name="ordered" value="false"/>
         <hfp:hasProperty name="bounded" value="false"/>
         <hfp:hasProperty name="cardinality" value="countably infinite"/>
         <hfp:hasProperty name="numeric" value="false"/>
      </xs:appinfo>
      <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#string"/>
   </xs:annotation>
   <xs:restriction base="xs:anySimpleType">
      <xs:whiteSpace value="preserve" id="string.preserve"/>
   </xs:restriction>
</xs:simpleType>

The Schema Adjunct Framework (SAF) proposes supplementing schemas with application-generation information. Its schema-adornment notation also uses xs:appinfo. Although not yet adapted to the W3C XML Schema recommendation, it could look like this; this is my own unofficial adaptation, only an example:

<xs:element name="author" type="authorType">
   <xs:annotation>
      <xs:appinfo source="saf:meta-data-item" xmlns:sql="http://www.extensibility.com/saf/spec/safsample/sql-map.saf">
         <sql:select>
            select
            <sql:elem>
               name
            </sql:elem>
            ,
            <sql:elem>
               birthdate
            </sql:elem>
            ,
            <sql:attr>
               deathdate
            </sql:attr>
            from tbl_author
         </sql:select>
      </xs:appinfo>
   </xs:annotation>
</xs:element>

SAF also embeds rules written as XPath expressions, also the domain of Schematron. Schematron rules can be embedded in xs:appinfo elements to check conditions W3C XML Schema cannot test, such as birth dates not following death dates.

How can this work when Schematron’s XPath 1.0 cannot interpret dates? This is hazardous, but if years have four digits and all dates the same time zone, as enforced by patterns in Regular expressions for specifying simple data types, ISO 8601 dates sort alphabetically.

<xs:element name="author" type="authorType">
   <xs:annotation>
      <xs:appinfo xmlns:sch="http://www.ascc.net/xml/schematron">
         <sch:pattern name="Born before dead">
            <sch:rule context="author">
               <sch:assert test="not(dead) or (dead &gt; born)" diagnostics="bornAfterDead">Ein Autor sollte erst nach seiner Geburt sterben.</sch:assert>
               <sch:diagnostics>
                  <sch:diagnostic id="bornAfterDead">Fehler, dieser Autor wurde erst nach seinem Tod geboren! Autor = <sch:value-of select="name"/> Geburt = <sch:value-of select="born"/> Tod = <sch:value-of select="dead"/></sch:diagnostic>
               </sch:diagnostics>
            </sch:rule>
         </sch:pattern>
      </xs:appinfo>
   </xs:annotation>
</xs:element>
                            

Though unusual, code fragments such as this XSLT-template may also be used:

<xs:element name="book" type="bookType">
   <xs:annotation>
      <xs:appinfo xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:template match="book">
            <xsl:apply-templates select="title"/>
            <xsl:apply-templates select="isbn"/>
            <p>Autoren:</p>
            <ul>
               <xsl:apply-templates select="author"/>
            </ul>
            <p>Figuren:</p>
            <ul>
               <xsl:apply-templates select="character"/>
            </ul>
         </xsl:template>
      </xs:appinfo>
   </xs:annotation>
</xs:element>

Resources can be linked instead of embedded, using XLink. RDDL, a vocabulary for describing namespaces, can supply these links outside its original purpose:

<xs:element name="author" type="authorType">
   <xs:annotation>
      <xs:appinfo xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rddl="http://www.rddl.org/">
         <rddl:resource id="author-transform" xlink:arcrole="http://www.w3.org/1999/xhtml" xlink:role="http://www.w3.org/1999/XSL/Transform" xlink:title="Author template" xlink:href="library.xslt#author">
            <div class="resource">
               <h4>XSLT-Transformation</h4>
               <p>Dieses <a href="library.xslt#author">Template</a> zeigt die Beschreibung eines Autors an.</p>
            </div>
         </rddl:resource>
         <rddl:resource id="CSS" xlink:title="CSS Stylesheet" xlink:role="http://www.isi.edu/in-notes/iana/assignments/media-types/text/css" xlink:href="author.css">
            <div class="resource">
               <h4>CSS-Stylesheet</h4>
               <p>Ein <a href="author.css">CSS-Stylesheet</a>, das die Stile definiert, die zur Anzeige eines Autors verwendet werden können.</p>
            </div>
         </rddl:resource>
      </xs:appinfo>
   </xs:annotation>
</xs:element>     

RDDL is pronounced “riddle”.

Both xs:documentation and description accept optional attribute source, a URI identifying the source or nature of the information. Since xs:documentation targets humans, optional attribute xml:lang may also be specified.

   

 

 

 

back to top