I'm using Qt C++ and am reading in an XML file for data. I want to ensure the XML file contains valid elements, so I began to write an XML Schema to validate against (Qt doesn't support validating against a DTD). The problem is, I'm not sure if my Schema itself is valid, and I can't seem to find an actual XSD validator. I tried using the official w3c validator at http://www.w3.org/2001/03/webdata/xsv, but it's giving me blank output. Would anyone know of a decent XSD validator, or perhaps be willing to look over the following manually?
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="ballot">
<xs:complexType>
<xs:sequence>
<xs:element name="races">
<xs:complexType>
<xs:element name="race" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="rtitle" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="candidates" minOccurs="1" maxOccurs="1">
<xs:complexType>
开发者_Python百科 <xs:element name="candidate" minOccurs="1" maxOccurs="10">
<xs:complexType>
<xs:element name="candname" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="candparty" minOccurs="1" maxOccurs="1" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
<xs:element name="propositions">
<xs:complexType>
<xs:element name="proposition" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:element name="ptitle" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="pdesc" minOccurs="1" maxOccurs="1" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Please let me know what you think, I appreciate it!
xs:element
can't go directly inside of xs:complexType
. Try this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="ballot">
<xs:complexType>
<xs:sequence>
<xs:element name="races">
<xs:complexType>
<xs:sequence>
<xs:element name="race" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="rtitle" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="candidates" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="candidate" minOccurs="1" maxOccurs="10">
<xs:complexType>
<xs:sequence>
<xs:element name="candname" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="candparty" minOccurs="1" maxOccurs="1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="propositions">
<xs:complexType>
<xs:sequence>
<xs:element name="proposition" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="ptitle" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="pdesc" minOccurs="1" maxOccurs="1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
It validates the following sample XML:
<?xml version="1.0" encoding="utf-8"?>
<ballot>
<races>
<race>
<rtitle>rtitle1</rtitle>
<candidates>
<candidate>
<candname>candname1</candname>
<candparty>candparty1</candparty>
</candidate>
<candidate>
<candname>candname2</candname>
<candparty>candparty2</candparty>
</candidate>
</candidates>
</race>
<race>
<rtitle>rtitle2</rtitle>
<candidates>
<candidate>
<candname>candname4</candname>
<candparty>candparty4</candparty>
</candidate>
</candidates>
</race>
</races>
<propositions>
<proposition>
<ptitle>ptitle1</ptitle>
<pdesc>pdesc1</pdesc>
</proposition>
<proposition>
<ptitle>ptitle2</ptitle>
<pdesc>pdesc2</pdesc>
</proposition>
<proposition>
<ptitle>ptitle3</ptitle>
<pdesc>pdesc3</pdesc>
</proposition>
</propositions>
</ballot>
精彩评论