如何使complexType为空或不超过100个值?

时间:2018-06-14 16:30:56

标签: xml xsd

这次我可以再次使用XML Schema来帮助你... 这是我必须要做的练习:

定义描述酒店的XML Schema。

Document's root is "hotel" element and it has following other elements:
1)name <hotel's name type:string>
2)rooms <list of "type" elements, which can be empty but with not more  
         than 100 elements of type RoomType which contain: 
                1)type <room's type, it's value is one of {single, double,triple}>
                2)number <number of rooms of specified type in the hotel>
3)forSmokers<optional element, number of rooms where smokers are allowed>

这是我的代码:

<?xml version = "1.0"?>
<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:element name = "hotel">
        <xs:complexType>
            <xs:sequence>
                <xs:element name = "name" type = "xs:string"/>
                <xs:element name = "rooms" type = "roomType"/>
                <xs:element name = "forSmokers" type = "xs:positiveInteger"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
        <xs:complexType name = "roomType">
            <xs:sequence>
                <xs:element name = "room'stype" type = "roomTypes" minOccurs = "0" maxOccurs = "100"/>
                <xs:element name = "number" type = "xs:positiveInteger" minOccurs = "0" maxOccurs = "100"/>
            </xs:sequence>
        </xs:complexType>
        <xs:simpleType name = "roomTypes">
            <xs:restriction base = "xs:string">
                <xs:enumeration value = "single"/>
                <xs:enumeration value = "double"/>
                <xs:enumeration value = "triple"/>
            </xs:restriction>
        </xs:simpleType>
</xs:schema>

现在使用这个XML文件,验证顺利进行:

<?xml version = "1.0"?>
<hotel>
    <name>NomeHotel</name>
    <rooms>
        <room'stype>singola</room'stype>
        <number>1</number>
    </rooms>
    <forSmokers>22</forSmokers>
</hotel>

我的问题是: 现在,如果我在现有<room'stype>代码中添加<number<rooms>代码,则验证会返回错误。 我需要房间标签允许0个元素(所以它可以是空的)但它不能超过100个元素,我如何修改代码才能实现呢?

很抱歉很长一段时间的问题,只是想清楚。 谢谢

2 个答案:

答案 0 :(得分:1)

您必须将架构更改为:

<?xml version = "1.0"?>
<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:element name = "hotel">
        <xs:complexType>
            <xs:sequence>
                <xs:element name = "name" type = "xs:string"/>
                <xs:element name = "rooms" type = "roomType"/>
                <xs:element name = "forSmokers" type = "xs:positiveInteger"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
        <xs:complexType name = "roomType">
            <xs:sequence>
                <xs:element name = "roomstype" type = "roomTypes" minOccurs = "0" maxOccurs = "100"/>
                <xs:element name = "number" type = "xs:positiveInteger" minOccurs = "0" maxOccurs = "100"/>
            </xs:sequence>
        </xs:complexType>
        <xs:simpleType name = "roomTypes">
            <xs:restriction base = "xs:string">
                <xs:enumeration value = "singola"/>
                <xs:enumeration value = "double"/>
                <xs:enumeration value = "triple"/>
            </xs:restriction>
        </xs:simpleType>
</xs:schema>

我注意到两件事:

  1. 元素名称不能包含'
  2. 在xml中有singola,但架构中有single

答案 1 :(得分:0)

XML元素名称不能包含撇号字符。

room'stype更改为roomsType或仅type,因为上下文应明确说明它是会议室的类型。

另请注意:您尚未向XML添加RoomType