需要一些我的XML Schema的帮助

时间:2010-04-14 19:34:51

标签: schema xsd

我正在使用Qt C ++并正在读取XML文件中的数据。我想确保XML文件包含有效元素,因此我开始编写XML Schema进行验证(Qt不支持验证DTD)。问题是,我不确定我的架构本身是否有效,而且我似乎找不到真正的XSD验证器。我尝试在http://www.w3.org/2001/03/webdata/xsv使用官方w3c验证器,但它给了我空白输出。有人会知道一个像样的XSD验证器,或者是否愿意手动查看以下内容?

    <?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>
                    <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>

请让我知道你的想法,我很感激!

1 个答案:

答案 0 :(得分:1)

xs:element无法直接进入xs:complexType。试试这个:

<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>

验证以下示例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>