How to define a specific number in xsd?

独自空忆成欢 提交于 2019-12-24 17:02:30

问题


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


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/10043414/how-to-define-a-specific-number-in-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!