maximus
maximus

Reputation: 11544

How to define a specific number in xsd?

i am working on an xsd-specification (for a simple game ;) ) which has that specification:

I have an Elem players with an attribute number, which indicates the number of the players(a num between 1 and 4). As childelems, it contains elems of zero to four screenname elements. These elements have player screenname text content and attribute, which indicates the end of the game number (a number between 1 and 4).

My big problems are the screenname and the Intervall in a typ in xsd? So how to do that?

greetings and thx in advance

Upvotes: 1

Views: 120

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21658

This is what I think you're describing:

<players number="2">
    <screenname endofgame="3">player screenname text content</screenname>
</players>

This would be an automatically generated XSD:

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="players">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="screenname">
          <xsd:complexType>
            <xsd:simpleContent>
              <xsd:extension base="xsd:string">
                <xsd:attribute name="endofgame" type="xsd:unsignedByte" use="required" />
              </xsd:extension>
            </xsd:simpleContent>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="number" type="xsd:unsignedByte" use="required" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

This would be one with additional constraints, as described: a num between 1 and 4 and zero to four screenname elements. By looking at before/after, you should understand which one is which.

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="players">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="screenname" minOccurs="0" maxOccurs="4">
          <xsd:complexType>
            <xsd:simpleContent>
              <xsd:extension base="xsd:string">
                <xsd:attribute name="endofgame" type="Int1to4" use="required" />
              </xsd:extension>
            </xsd:simpleContent>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="number" type="Int1to4" use="required" />
    </xsd:complexType>
  </xsd:element>
  <xsd:simpleType name="Int1to4">
    <xsd:restriction base="xsd:int">
      <xsd:minInclusive value="1"/>
      <xsd:maxInclusive value="4"/>
    </xsd:restriction>  
  </xsd:simpleType> 
</xsd:schema>

Upvotes: 1

Nick Ryan
Nick Ryan

Reputation: 2670

To specify the amount of recurrence of an element you need to use the minOccurs and maxOccurs attributes.

Upvotes: 0

Related Questions