Files
JIMRI/help/en/html/doc/Technical/XmlSchemaExamples.shtml
2026-06-17 14:00:51 +02:00

158 lines
4.8 KiB
Plaintext

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="generator" content="HTML Tidy for HTML5 for Apple macOS version 5.8.0">
<title>JMRI: XML Schema Examples</title>
<meta name="author" content="Bob Jacobsen">
<meta name="keywords" content="JMRI technical code xml schema usage">
<!--#include virtual="/help/en/parts/Style.shtml" -->
</head>
<body>
<!--#include virtual="/help/en/parts/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>
<code class="block">
&lt;xs:element name="someData" minOccurs="0" maxOccurs="1"&gt;
</code>
That doesn't specify any typing. If you want e.g. to enforce integer:
<code class="block">
&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>
<h2>Element with just attributes, no content</h2>
Preferred Venetion-blind form:
<code class="block">
&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>
Can also be combined if you think it's unlikely to be used elsewhere:
<code class="block">
&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>
<h2>Element with text content and attributes</h2>
Restricting the content of the element to just an integer:
<code class="block">
&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>
<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.
<code class="block">
&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>
Then putting it on an attribute is simple:
<code class="block">
&lt;xs:attribute name="opsOnly" type="yesNoType"/&gt;
</code>
<h2>Element with restricted text content</h2>
Not an attribute, an element:
<code class="block">
&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>
<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:
<code class="block">
&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>
and example use in some later type:
<code class="block">
&lt;xs:attributeGroup ref="EditorCommonAttributesGroup" /&gt;
</code>
<!--#include virtual="/help/en/parts/Footer.shtml" -->
</div>
<!-- closes #mainContent-->
</div>
<!-- closes #mBody-->
<script src="/js/help.js"></script>
</body>
</html>