Chapter 3: XML Schema

B4J tool works on a grammar definition using an XML Schema subset. In a nutshell, you have to declare all elements first, then all corresponding complex types. Complex types may not be embedded in one another, and have basically attributes and three possible contents:

Choices and sequences can not be mixed in the current version, and may never be in potential future releases.

Declaring allowed Vocaburary

First, declare all element names that appear in game data, starting with root element quizz. We don't consider attributes for now.
Each element declaration uses following syntax:

<xsd:element name="elementName" type="seq or ch or st"/>

type attribute is used in XML Schema to reference the corresponding complex type,

e.g. gmr:ElementName
The "gmr" prefix, associated with your game namespace "http://www.example.com/Guess_Me_Right", is always included in references (for complex types, attributes, etc...).

In this tutorial, we use an intermediate syntax: simply write type="seq" for sequences, type="ch" for choices, and type="s" for simple content elements (with text content).
Cheat: HERE is corresponding xml schema.

Generating Complex Types for Elements

ant addSchemaTypes
generates template complex types according to your previous declarations. Based on our conventions, each complex type has the same name as corresponding element, different only by first capital letter. THIS is the resulting file.

Adding attributes and children

You should now add attributes declarations, using following syntax:

<xsd:attribute name="attributeName" type="xsd:byte" />
Attribute name is free; for an attributeName attribute, an atAttributeName variable will contain decoded value, and an isAtAttributeNamePresent boolean will be available if it is an optional one.
Following table lists supported types with corresponding Java type:
XML SchemaJava
xsd:booleanboolean
xsd:bytebyte
xsd:shortshort
xsd:intint
xsd:unsignedIntint
xsd:longlong
xsd:floatint*
xsd:doubledouble
xsd:stringString (UTF-8)
mathFP:floatint (encoded with MathFP)
mathFP:radAngleint (encoded with MathFP)
mathFP:cosRadAngleint (encoded with MathFP)
mathFP:sinRadAngleint (encoded with MathFP)
mathFP:minusCosRadAngleint (encoded with MathFP)
mathFP:minusSinRadAngleint (encoded with MathFP)
include:Idbyte
include:IdRefbyte
ignore:IdNone
ignore:IdRefNone
*: was int before mathFP:float was available, should become float.
Cheat: THIS is resulting file

Validate Complete Grammar and Data

Finally, your schema is now VALID! You can use it to validate your game data, using

ant validateData
but you should first check it is a valid XML Schema subset for B4J using
ant validateSchema

Next

Go on to Chapter 4...
or back to Chapter 1 or Chapter 2.