Pages

Friday, April 22, 2011

Define empty elements in XSD

It's sometimes useful to include "empty" elements in your document. These are elements that only have attributes, no subelements or other textual content. For example, the following copyright element only contains a date attribute:

<copyright date="2004-10-15"/>

In XSD, define the empty content model as follows:

<xsd:element name="copyright">
  <xsd:complexType>
    <xsd:attribute name="date" type="xsd:date"/>
  </xsd:complexType>
</xsd:element>

Note that there's no child element in this content model, only an attribute name. The attribute uses the xsd:date datatype to constrain the value of the attribute to the lexical date (CCYY-MM-DD). If you don't want to constrain the date value, you can use xsd:string instead. You can now insert a copyright tag with a date attribute in your document without specifying additional content.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.