<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
  <meta name="generator" content=
  "HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15.17), see www.w3.org">

  <title>JMRI: XML Schema Examples</title>
    <meta name="author" content="Bob Jacobsen">
  <meta name="keywords" content=
  "JMRI technical code xml schema usage">
  <!-- The combination of "Define" and {Header,Style, Logo and Footer} comments -->
  <!-- are an arbitrary design pattern used by the update.pl script to -->
  <!-- easily replace the common header/footer code for all the web pages -->
  <!-- delete the following 2 Defines if you want to use the default JMRI logo -->
  <!-- or change them to reflect your alternative logo -->
  <!--#include virtual="/Style.shtml" -->
</head>

<body>
<!--#include virtual="/Header.shtml" -->
<div id="mBody">
    <!--#include virtual="Sidebar.shtml" -->
    <div id="mainContent">

    <h1>JMRI Code: XML Schema Examples</h1>
      <p>This page contains examples of various XML Schema fragments
      that you might find useful.<br>
      For discussion of JMRI's use of XML Schema, including info on
      preferred patterns and organization, see <a href=
      "XmlSchema.shtml">another page</a>.</p>

      <h2>Element with just text content, no attributes</h2>
      <pre>
<code>
&lt;xs:element name="someData" minOccurs="0" maxOccurs="1"&gt;
</code>
</pre>That doesn't specify any typing. If you want e.g. to enforce
integer:
      <pre>
<code>
&lt;xs:element name="someIntThing" &gt;
  &lt;xs:complexType&gt;
    &lt;xs:simpleContent&gt;
      &lt;xs:extension base="xs:int" /&gt;
    &lt;/xs:simpleContent&gt;
  &lt;/xs:complexType&gt;
&lt;/xs:element&gt;
</code>
</pre>

      <h2>Element with just attributes, no content</h2>Preferred
      Venetion-blind form:
      <pre>
<code>
&lt;xs:element name="sample" type="SampleType" 
    minOccurs="0" maxOccurs="unbounded" /&gt;

&lt;xs:complexType name="SampleType"&gt;
  &lt;xs:attribute name="foo" /&gt;
  &lt;xs:attribute name="bar" /&gt;
&lt;/xs:complexType&gt;
</code>
</pre>Can also be combined if you think it's unlikely to be used
elsewhere:
      <pre>
<code>
&lt;xs:element name="sample" 
    minOccurs="0" maxOccurs="unbounded" /&gt;
  &lt;xs:complexType&gt;
    &lt;xs:attribute name="foo" /&gt;
    &lt;xs:attribute name="bar" /&gt;
  &lt;/xs:complexType&gt;
&lt;/xs:element&gt;
</code>
</pre>

      <h2>Element with text content and attributes</h2>Restricting
      the content of the element to just an integer:
      <pre>
<code>
&lt;xs:element name="someIntThing" &gt;
  &lt;xs:complexType&gt;
    &lt;xs:simpleContent&gt;
      &lt;xs:extension base="xs:int"&gt;
        &lt;xs:attribute name="someInt" type="xs:int"/&gt;
        &lt;xs:attribute name="someText" type="xs:string"/&gt;
      &lt;/xs:extension&gt;
    &lt;/xs:simpleContent&gt;
  &lt;/xs:complexType&gt;
&lt;/xs:element&gt;
</code>
</pre>

      <h2>Limiting an attribute to some specific values</h2>If you
      want to do this, it's worth it to define a general type that
      can be reused. These live in xml/schema/types/general.xsd.
      <pre>
<code>
&lt;xs:simpleType name="yesNoType"&gt;
  &lt;xs:annotation&gt;
    &lt;xs:documentation&gt;
      General definition of string that's either "yes" or "no".
    &lt;/xs:documentation&gt;
  &lt;/xs:annotation&gt;
  &lt;xs:restriction base="xs:token"&gt;
    &lt;xs:enumeration value="yes"/&gt;
    &lt;xs:enumeration value="no"/&gt;
  &lt;/xs:restriction&gt;
&lt;/xs:simpleType&gt;
</code>
</pre>Then putting it on an attribute is simple:
      <pre>
<code>
&lt;xs:attribute name="opsOnly" type="yesNoType"/&gt;
</code>
</pre>

      <h2>Element with restricted text content</h2>Not an
      attribute, an element:
      <pre>
<code>
&lt;xs:element name="relation"&gt;
  &lt;xs:simpleType&gt;
    &lt;xs:restriction base="xs:string"&gt;
      &lt;xs:enumeration value="ge"/&gt;
      &lt;xs:enumeration value="lt"/&gt;
      &lt;xs:enumeration value="eq"/&gt;
      &lt;xs:enumeration value="ne"/&gt;
    &lt;/xs:restriction&gt;
  &lt;/xs:simpleType&gt;
&lt;/xs:element&gt;
</code>
</pre>

      <h2>Attribute Groups</h2>Attribute Groups are good for
      representing a set of attributes read and written together by
      a common service routine. Example definition:
      <pre>
<code>
&lt;xs:attributeGroup name="EditorCommonAttributesGroup"&gt;
  &lt;xs:annotation&gt;
    &lt;xs:documentation&gt;
      Define the XML stucture for storing common PositionableLabel child attributes
    &lt;/xs:documentation&gt;
    &lt;xs:appinfo&gt;
      jmri.jmrit.display.configurexml.PositionableLabelXml#storeCommonAttributes
    &lt;/xs:appinfo&gt;
  &lt;/xs:annotation&gt;
  &lt;xs:attribute name="x" type="xs:int" use="required" /&gt;
  &lt;xs:attribute name="y" type="xs:int" use="required" /&gt;
  &lt;xs:attribute name="level" type="xs:int" /&gt;
  &lt;xs:attribute name="forcecontroloff" type="trueFalseType" default="false" /&gt;
&lt;/xs:attributeGroup&gt;
</code>
</pre>and example use in some later type:
      <pre>
<code>
&lt;xs:attributeGroup ref="EditorCommonAttributesGroup" /&gt;
</code>
</pre>

        <!--#include virtual="/Footer.shtml" -->
    </div><!-- closes #mainContent-->
  </div><!-- closes #mBody-->
</body>
</html>
