【问题标题】:Need some help with my XML Schema需要一些关于我的 XML Schema 的帮助
【发布时间】:2010-04-15 03:41:34
【问题描述】:

我正在使用 Qt C++ 并正在读取 XML 文件中的数据。我想确保 XML 文件包含有效元素,所以我开始编写一个 XML Schema 来验证(Qt 不支持验证 DTD)。问题是,我不确定我的 Schema 本身是否有效,而且我似乎找不到真正的 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>

请告诉我你的想法,我很感激!

【问题讨论】:

    标签: schema xsd


    【解决方案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>
    

    【讨论】:

    • 太好了,非常感谢您的帮助! w3schools 解释顺序的方式,我认为这是顺序的可选要求。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 2013-08-31
    • 1970-01-01
    相关资源
    最近更新 更多