JMRI: XML Schema Examples
Author: Bob Jacobsen
Keywords: JMRI technical code xml schema usage

# JMRI Code: XML Schema Examples

This page contains examples of various XML Schema fragments that you might find useful.
For discussion of JMRI's use of XML Schema, including info on preferred patterns and organization, see another page.

## Element with just text content, no attributes

`<xs:element name="someData" minOccurs="0" maxOccurs="1">` That doesn't specify any typing. If you want e.g. to enforce integer: `<xs:element name="someIntThing" > <xs:complexType> <xs:simpleContent> <xs:extension base="xs:int" /> </xs:simpleContent> </xs:complexType> </xs:element>`

## Element with just attributes, no content

Preferred Venetion-blind form: `<xs:element name="sample" type="SampleType" minOccurs="0" maxOccurs="unbounded" /> <xs:complexType name="SampleType"> <xs:attribute name="foo" /> <xs:attribute name="bar" /> </xs:complexType>` Can also be combined if you think it's unlikely to be used elsewhere: `<xs:element name="sample" minOccurs="0" maxOccurs="unbounded" /> <xs:complexType> <xs:attribute name="foo" /> <xs:attribute name="bar" /> </xs:complexType> </xs:element>`

## Element with text content and attributes

Restricting the content of the element to just an integer: `<xs:element name="someIntThing" > <xs:complexType> <xs:simpleContent> <xs:extension base="xs:int"> <xs:attribute name="someInt" type="xs:int"/> <xs:attribute name="someText" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>`

## Limiting an attribute to some specific values

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. `<xs:simpleType name="yesNoType"> <xs:annotation> <xs:documentation> General definition of string that's either "yes" or "no". </xs:documentation> </xs:annotation> <xs:restriction base="xs:token"> <xs:enumeration value="yes"/> <xs:enumeration value="no"/> </xs:restriction> </xs:simpleType>` Then putting it on an attribute is simple: `<xs:attribute name="opsOnly" type="yesNoType"/>`

## Element with restricted text content

Not an attribute, an element: `<xs:element name="relation"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="ge"/> <xs:enumeration value="lt"/> <xs:enumeration value="eq"/> <xs:enumeration value="ne"/> </xs:restriction> </xs:simpleType> </xs:element>`

## Attribute Groups

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