validating an XML schema with empty attributes

三世轮回 提交于 2019-12-19 04:09:52

问题


I am having trouble validating my xml schema.

I get these errors on the schema

113: 18 s4s-elt-invalid-content.1: The content of '#AnonType_user' is invalid.

164: 17 s4s-elt-invalid-content.1: The content of '#AnonType_festival' is invalid. Element 'sequence' is invalid, misplaced, or occurs too often.

and becuase of those 2 errors, i am getting loads of the same error. This is becuase the attribute id of the festival tag may be empty becuase there is not data for that festival

cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'.

cvc-attribute.3: The value '' of attribute 'id' on element 'festival' is not valid with respect to its type, 'integer'.

The lines in the schema causing the problems are

<xs:element name="user">
  <xs:complexType>
 <xs:attribute name="id" type="xs:integer"/>
   <xs:sequence>
  <xs:element ref="personal"/>
  <xs:element ref="account"/> 
   </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="festival">
  <xs:complexType>
 <xs:attribute name="id" type="xs:integer" user="optional"/>
  <xs:sequence>
   <xs:element ref="event"/>
   <xs:element ref="contact"/> 
 </xs:sequence>
  </xs:complexType>
</xs:element>

This is a snippet from my XML file. One user has a festival and the other doesn't

<member>
 <user id="3">
  <personal>
   <name>Skye Saunders</name>
   <sex>Female</sex>
   <address1>31 Anns Court</address1>
   <address2></address2>
   <city>Cirencester</city>
   <county>Gloucestershire</county>
   <postcode>GL7 1JG</postcode>
   <telephone>01958303514</telephone>
   <mobile>07260491667</mobile>
   <email>bigundecided@hotmail.co.uk</email>
  </personal>
  <account>
   <username>BigUndecided</username>
   <password>ea297847f80e046ca24a8621f4068594</password>
   <userlevel>2</userlevel>
   <signupdate>2010-03-26T09:23:50</signupdate>
  </account>
 </user>
 <festival id="">
  <event>
   <eventname></eventname>
   <url></url>
   <datefrom></datefrom>
   <dateto></dateto>
   <location></location>
   <eventpostcode></eventpostcode>
   <coords>
    <lat></lat>
    <lng></lng>
   </coords>
  </event>
  <contact>
   <conname></conname>
   <conaddress1></conaddress1>
   <conaddress2></conaddress2>
   <concity></concity>
   <concounty></concounty>
   <conpostcode></conpostcode>
   <contelephone></contelephone>
   <conmobile></conmobile>
   <fax></fax>
   <conemail></conemail>
  </contact>
 </festival>
</member>
<member>
 <user id="4">
  <personal>
   <name>Connor Lawson</name>
   <sex>Male</sex>
   <address1>12 Ash Way</address1>
   <address2></address2>
   <city>Swindon</city>
   <county>Wiltshire</county>
   <postcode>SN3  6GS</postcode>
   <telephone>01791928119</telephone>
   <mobile>07338695664</mobile>
   <email>itunestinker@fsmail.co.uk</email>
  </personal>
  <account>
   <username>iTuneStinker</username>
   <password>3a1f5fda21a07bfff20c41272bae7192</password>
   <userlevel>3</userlevel>
   <signupdate>2010-03-26T09:23:50</signupdate>
  </account>
 </user>
 <festival id="1">
  <event>
   <eventname>Oxford Folk Festival</eventname>
   <url>http://www.oxfordfolkfestival.com/</url>
   <datefrom>2010-04-07</datefrom>
   <dateto>2010-04-09</dateto>
   <location>Oxford</location>
   <eventpostcode>OX19BE</eventpostcode>
   <coords>
    <lat>51.735640</lat>
    <lng>-1.276136</lng>
   </coords>
  </event>
  <contact>
   <conname>Stuart Vincent</conname>
   <conaddress1>P.O. Box 642</conaddress1>
   <conaddress2></conaddress2>
   <concity>Oxford</concity>
   <concounty>Bedfordshire</concounty>
   <conpostcode>OX13BY</conpostcode>
   <contelephone>01865 79073</contelephone>
   <conmobile></conmobile>
   <fax></fax>
   <conemail>stewards@oxfordfolkfestival.com</conemail>
  </contact>
 </festival>
</member>

This is my schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:simpleType name="postcode">
  <xs:restriction base="xs:string">
   <xs:minLength value="6"/>
   <xs:maxLength value="8"/>
  </xs:restriction>
 </xs:simpleType>

 <xs:simpleType name="telephone">
  <xs:restriction base="xs:string">
   <xs:minLength value="10"/>
   <xs:maxLength value="13"/>
  </xs:restriction>
 </xs:simpleType>

 <xs:simpleType name="mobile">
  <xs:restriction base="xs:string">
   <xs:minLength value="11"/>
   <xs:maxLength value="11"/>
  </xs:restriction>
 </xs:simpleType>

 <xs:simpleType name="password">
  <xs:restriction base="xs:string">
   <xs:minLength value="32"/>
   <xs:maxLength value="32"/>
  </xs:restriction>
 </xs:simpleType>

 <xs:simpleType name="userlevel">
  <xs:restriction base="xs:integer">
   <xs:enumeration value="1"/>
   <xs:enumeration value="2"/>
   <xs:enumeration value="3"/>
   <xs:enumeration value="4"/>
  </xs:restriction>
 </xs:simpleType>

 <xs:simpleType name="county">
  <xs:restriction base="xs:string">
   <xs:enumeration value="Bedfordshire"/>
   <xs:enumeration value="Berkshire"/>
   <xs:enumeration value="Bristol"/>
   <xs:enumeration value="Buckinghamshire"/>
   <xs:enumeration value="Cambridgeshire"/>
   <xs:enumeration value="Cheshire"/>
   <xs:enumeration value="Cleveland"/>
   <xs:enumeration value="Cornwall"/>
   <xs:enumeration value="Cumberland"/>
   <xs:enumeration value="Derbyshire"/>
   <xs:enumeration value="Devon"/>
   <xs:enumeration value="Dorset"/>
   <xs:enumeration value="Durham"/>
   <xs:enumeration value="East Ridings Of Yorkshire"/>
   <xs:enumeration value="Essex"/>
   <xs:enumeration value="Gloucestershire"/>
   <xs:enumeration value="Hampshire"/>
   <xs:enumeration value="Herefordshire"/>
   <xs:enumeration value="Hertfordshire"/>
   <xs:enumeration value="Huntingdonshire"/>
   <xs:enumeration value="Isle Of Man"/>
   <xs:enumeration value="Kent"/>
   <xs:enumeration value="Lancashire"/>
   <xs:enumeration value="Leicestershire"/>
   <xs:enumeration value="Lincolnshire"/>
   <xs:enumeration value="London"/>
   <xs:enumeration value="Middlesex"/>
   <xs:enumeration value="Norfolk"/>
   <xs:enumeration value="North Yorkshire"/>
   <xs:enumeration value="Northamptonshire"/>
   <xs:enumeration value="Northumberland"/>
   <xs:enumeration value="Nottinghamshire"/>
   <xs:enumeration value="Oxfordshire"/>
   <xs:enumeration value="Rutland"/>
   <xs:enumeration value="Shropshire"/>
   <xs:enumeration value="Somerset"/>
   <xs:enumeration value="South Yorkshire"/>
   <xs:enumeration value="Staffordshire"/>
   <xs:enumeration value="Suffolk"/>
   <xs:enumeration value="Surrey"/>
   <xs:enumeration value="Sussex"/>
   <xs:enumeration value="Tyne and Wear"/>
   <xs:enumeration value="Warwickshire"/>
   <xs:enumeration value="West Yorkshire"/>
   <xs:enumeration value="Westmorland"/>
   <xs:enumeration value="Wiltshire"/>
   <xs:enumeration value="Wirral"/>
   <xs:enumeration value="Worcestershire"/>
   <xs:enumeration value="Yorkshire"/>
  </xs:restriction>
 </xs:simpleType> 

 <xs:element name="folktask">
  <xs:complexType>
   <xs:sequence>
    <xs:element minOccurs="0" maxOccurs="unbounded" ref="member"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="member">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="user" minOccurs="0" maxOccurs="unbounded"/>
    <xs:element ref="festival" minOccurs="0" maxOccurs="unbounded"/>  
   </xs:sequence>
  </xs:complexType>
 </xs:element>

 <xs:element name="user">
   <xs:complexType>
  <xs:attribute name="id" type="xs:integer"/>
    <xs:sequence>
   <xs:element ref="personal"/>
   <xs:element ref="account"/> 
    </xs:sequence>
   </xs:complexType>
 </xs:element>

 <xs:element name="personal">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="name"/>
    <xs:element ref="sex"/>
    <xs:element ref="address1"/>
    <xs:element ref="address2"/>
    <xs:element ref="city"/>
    <xs:element ref="county"/>
    <xs:element ref="postcode"/>
    <xs:element ref="telephone"/>
    <xs:element ref="mobile"/>
    <xs:element ref="email"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="name" type="xs:string"/>
 <xs:element name="sex" type="xs:string"/>
 <xs:element name="address1" type="xs:string"/>
 <xs:element name="address2" type="xs:string"/>
 <xs:element name="city" type="xs:string"/>
 <xs:element name="county" type="xs:string"/>
 <xs:element name="postcode" type="postcode"/>
 <xs:element name="telephone" type="telephone"/>
 <xs:element name="mobile" type="mobile"/>
 <xs:element name="email" type="xs:string"/>
 <xs:element name="account">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="username"/>
    <xs:element ref="password"/>
    <xs:element ref="userlevel"/>
    <xs:element ref="signupdate"/> 
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="username" type="xs:string"/>
 <xs:element name="password" type="password"/>
 <xs:element name="userlevel" type="userlevel"/>
 <xs:element name="signupdate" type="xs:dateTime"/>

 <xs:element name="festival">
   <xs:complexType>
  <xs:attribute name="id" type="xs:integer" user="optional"/>
   <xs:sequence>
    <xs:element ref="event"/>
    <xs:element ref="contact"/> 
  </xs:sequence>
   </xs:complexType>
 </xs:element>

 <xs:element name="event">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="eventname"/> 
    <xs:element ref="url"/>
    <xs:element ref="datefrom"/>
    <xs:element ref="dateto"/>
    <xs:element ref="location"/>
    <xs:element ref="eventpostcode"/>
    <xs:element ref="coords"/> 
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="eventname" type="xs:string"/>
 <xs:element name="url" type="xs:string"/>
 <xs:element name="datefrom" type="xs:date"/>
 <xs:element name="dateto" type="xs:date"/>
 <xs:element name="location" type="xs:string"/>
 <xs:element name="eventpostcode" type="postcode"/>
 <xs:element name="coords">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="lat"/>
    <xs:element ref="lng"/> 
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="lat" type="xs:decimal"/>
 <xs:element name="lng" type="xs:decimal"/>
 <xs:element name="contact">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="conname"/>
    <xs:element ref="conaddress1"/>
    <xs:element ref="conaddress2"/>
    <xs:element ref="concity"/> 
    <xs:element ref="concounty"/>
    <xs:element ref="conpostcode"/> 
    <xs:element ref="contelephone"/>
    <xs:element ref="conmobile"/> 
    <xs:element ref="fax"/>
    <xs:element ref="conemail"/>  
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="conname" type="xs:string"/>
 <xs:element name="conaddress1" type="xs:string"/>
 <xs:element name="conaddress2" type="xs:string"/>
 <xs:element name="concity" type="xs:string"/>
 <xs:element name="concounty" type="xs:string"/>
 <xs:element name="conpostcode" type="postcode"/>
 <xs:element name="contelephone" type="telephone"/>
 <xs:element name="conmobile" type="mobile"/>
 <xs:element name="fax" type="telephone"/>
 <xs:element name="conemail" type="xs:string"/>
</xs:schema>

回答1:


This is a valid schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:simpleType name="postcode">
        <xs:restriction base="xs:string">
            <xs:minLength value="6"/>
            <xs:maxLength value="8"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="telephone">
        <xs:restriction base="xs:string">
            <xs:minLength value="10"/>
            <xs:maxLength value="13"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="mobile">
        <xs:restriction base="xs:string">
            <xs:minLength value="11"/>
            <xs:maxLength value="11"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="password">
        <xs:restriction base="xs:string">
            <xs:minLength value="32"/>
            <xs:maxLength value="32"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="userlevel">
        <xs:restriction base="xs:integer">
            <xs:enumeration value="1"/>
            <xs:enumeration value="2"/>
            <xs:enumeration value="3"/>
            <xs:enumeration value="4"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="county">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Bedfordshire"/>
            <xs:enumeration value="Berkshire"/>
            <xs:enumeration value="Bristol"/>
            <xs:enumeration value="Buckinghamshire"/>
            <xs:enumeration value="Cambridgeshire"/>
            <xs:enumeration value="Cheshire"/>
            <xs:enumeration value="Cleveland"/>
            <xs:enumeration value="Cornwall"/>
            <xs:enumeration value="Cumberland"/>
            <xs:enumeration value="Derbyshire"/>
            <xs:enumeration value="Devon"/>
            <xs:enumeration value="Dorset"/>
            <xs:enumeration value="Durham"/>
            <xs:enumeration value="East Ridings Of Yorkshire"/>
            <xs:enumeration value="Essex"/>
            <xs:enumeration value="Gloucestershire"/>
            <xs:enumeration value="Hampshire"/>
            <xs:enumeration value="Herefordshire"/>
            <xs:enumeration value="Hertfordshire"/>
            <xs:enumeration value="Huntingdonshire"/>
            <xs:enumeration value="Isle Of Man"/>
            <xs:enumeration value="Kent"/>
            <xs:enumeration value="Lancashire"/>
            <xs:enumeration value="Leicestershire"/>
            <xs:enumeration value="Lincolnshire"/>
            <xs:enumeration value="London"/>
            <xs:enumeration value="Middlesex"/>
            <xs:enumeration value="Norfolk"/>
            <xs:enumeration value="North Yorkshire"/>
            <xs:enumeration value="Northamptonshire"/>
            <xs:enumeration value="Northumberland"/>
            <xs:enumeration value="Nottinghamshire"/>
            <xs:enumeration value="Oxfordshire"/>
            <xs:enumeration value="Rutland"/>
            <xs:enumeration value="Shropshire"/>
            <xs:enumeration value="Somerset"/>
            <xs:enumeration value="South Yorkshire"/>
            <xs:enumeration value="Staffordshire"/>
            <xs:enumeration value="Suffolk"/>
            <xs:enumeration value="Surrey"/>
            <xs:enumeration value="Sussex"/>
            <xs:enumeration value="Tyne and Wear"/>
            <xs:enumeration value="Warwickshire"/>
            <xs:enumeration value="West Yorkshire"/>
            <xs:enumeration value="Westmorland"/>
            <xs:enumeration value="Wiltshire"/>
            <xs:enumeration value="Wirral"/>
            <xs:enumeration value="Worcestershire"/>
            <xs:enumeration value="Yorkshire"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="folktask">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" ref="member"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="member">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="user" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="festival" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="user">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="personal"/>
                <xs:element ref="account"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:integer"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="personal">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="name"/>
                <xs:element ref="sex"/>
                <xs:element ref="address1"/>
                <xs:element ref="address2"/>
                <xs:element ref="city"/>
                <xs:element ref="county"/>
                <xs:element ref="postcode"/>
                <xs:element ref="telephone"/>
                <xs:element ref="mobile"/>
                <xs:element ref="email"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="sex" type="xs:string"/>
    <xs:element name="address1" type="xs:string"/>
    <xs:element name="address2" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="county" type="xs:string"/>
    <xs:element name="postcode" type="postcode"/>
    <xs:element name="telephone" type="telephone"/>
    <xs:element name="mobile" type="mobile"/>
    <xs:element name="email" type="xs:string"/>
    <xs:element name="account">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="username"/>
                <xs:element ref="password"/>
                <xs:element ref="userlevel"/>
                <xs:element ref="signupdate"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="username" type="xs:string"/>
    <xs:element name="password" type="password"/>
    <xs:element name="userlevel" type="userlevel"/>
    <xs:element name="signupdate" type="xs:dateTime"/>
    <xs:element name="festival">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="event"/>
                <xs:element ref="contact"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:integer" use="optional"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="event">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="eventname"/>
                <xs:element ref="url"/>
                <xs:element ref="datefrom"/>
                <xs:element ref="dateto"/>
                <xs:element ref="location"/>
                <xs:element ref="eventpostcode"/>
                <xs:element ref="coords"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="eventname" type="xs:string"/>
    <xs:element name="url" type="xs:string"/>
    <xs:element name="datefrom" type="xs:date"/>
    <xs:element name="dateto" type="xs:date"/>
    <xs:element name="location" type="xs:string"/>
    <xs:element name="eventpostcode" type="postcode"/>
    <xs:element name="coords">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="lat"/>
                <xs:element ref="lng"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="lat" type="xs:decimal"/>
    <xs:element name="lng" type="xs:decimal"/>
    <xs:element name="contact">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="conname"/>
                <xs:element ref="conaddress1"/>
                <xs:element ref="conaddress2"/>
                <xs:element ref="concity"/>
                <xs:element ref="concounty"/>
                <xs:element ref="conpostcode"/>
                <xs:element ref="contelephone"/>
                <xs:element ref="conmobile"/>
                <xs:element ref="fax"/>
                <xs:element ref="conemail"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="conname" type="xs:string"/>
    <xs:element name="conaddress1" type="xs:string"/>
    <xs:element name="conaddress2" type="xs:string"/>
    <xs:element name="concity" type="xs:string"/>
    <xs:element name="concounty" type="xs:string"/>
    <xs:element name="conpostcode" type="postcode"/>
    <xs:element name="contelephone" type="telephone"/>
    <xs:element name="conmobile" type="mobile"/>
    <xs:element name="fax" type="telephone"/>
    <xs:element name="conemail" type="xs:string"/>
</xs:schema>

Explanation:

The <sequence> tag had been misplaced

You also had a typo where you wrote user instead of use as an attribute to an <attribute> tag



来源:https://stackoverflow.com/questions/2629041/validating-an-xml-schema-with-empty-attributes

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