xsd的验证错误

时间:2014-01-04 21:20:10

标签: xml validation xsd

我有以下xml:

<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=
                      "http://www.books.org
                       BookStore.xsd">
<Book category="Life style">
    <Status>Available</Status>
    <Title lang="en">Hour before dawn</Title>
    <Author>
        <Name>Paul McCartney</Name>
        <Genre>M</Genre>
    </Author>
    <Date>
        <Day>23</Day>
        <Month>03</Month>
        <Year>1999</Year>
    </Date>
    <Price type="euro">29.99</Price>
    <ISBN>1-56592-245-2</ISBN>
    <Publisher>McMilli Publishing</Publisher>
</Book>
<Book category="Philosophy">
    <Status>Not available</Status>
    <Title lang="de">Thus spoke Zarathustra</Title>
    <Author>
        <Name>Friedrich Nietzsche</Name>
        <Genre>M</Genre>
    </Author>
    <Date>
        <Day>21</Day>
        <Month>11</Month>
        <Year>1877</Year>
    </Date>
    <Price type="euro">39.15</Price>
    <ISBN>0-440-34319-4</ISBN>
    <Publisher>Sell Publishing Co.</Publisher>
</Book>
<Book category="Fiction">
    <Status>Available</Status>
    <Title lang="en">To kill a mockingbird</Title>
    <Author>
        <Name>Harper Lee</Name>
        <Genre>M</Genre>
    </Author>
    <Date>
        <Day>15</Day>
        <Month>03</Month>
        <Year>1982</Year>
    </Date>
    <Price type="euro">35.15</Price>
    <ISBN>0-454-25215-8</ISBN>
    <Publisher>Harper Row</Publisher>
</Book>
<Book category="Fantasy">
    <Status>Available</Status>
    <Title lang="en">Hexed</Title>
    <Author>
        <Name>Ilona Andrews</Name>
        <Genre>F</Genre>
    </Author>
    <Author>
        <Name>Yasmine Galenorn</Name>
        <Genre>F</Genre>
    </Author>
    <Author>
        <Name>Allyson James</Name>
        <Genre>F</Genre>
    </Author>
    <Author>
        <Name>Jeanne Stein</Name> 
        <Genre>F</Genre>
    </Author>
    <Date>
        <Day>01</Day>
        <Month>01</Month>
        <Year>2011</Year>
    </Date>
    <Price type="euro">19.8</Price>
    <ISBN>3-521-77423-9</ISBN>
    <Publisher>Harper &amp; Row</Publisher>
</Book>
<Book category="Web">
    <Status>Not available</Status>
    <Title lang="en">SCJP Sun certified programmer for Java 6</Title>
    <Author>
        <Name>Kathy Sierra</Name>
        <Genre>F</Genre>
    </Author>
    <Author>
        <Name>Bert Bates</Name>
        <Genre>M</Genre>
    </Author>
    <Date>
        <Day>06</Day>
        <Month>09</Month>
        <Year>2011</Year>
    </Date>
    <Price type="euro">55</Price>
    <ISBN>2-421-52214-1</ISBN>
    <Publisher>Learnkey</Publisher>
</Book>
<Book category="Horror">
    <Status>Available</Status>
    <Title lang="en">Out of the depths</Title>
    <Author>
        <Name>Cathy MacPhail</Name>
        <Genre>F</Genre>
    </Author>
    <Date>
        <Day>08</Day>
        <Month>12</Month>
        <Year>2013</Year>
    </Date>
    <Price type="euro">41</Price>
    <ISBN>7-666-21242-7</ISBN>
    <Publisher>Greenock</Publisher>
</Book>
<Book category="Art and design">
    <Status>Available</Status>
    <Title lang="en">The design of everyday things</Title>
    <Author>
        <Name>Donald Norman</Name>
        <Genre>M</Genre>
    </Author>
    <Date>
        <Day>27</Day>
        <Month>02</Month>
        <Year>2002</Year>
    </Date>
    <Price type="euro">26</Price>
    <ISBN>8-4322-62332-6</ISBN>
    <Publisher>Basic books</Publisher>
</Book>

我在线验证了xml,它形成得很好。接下来我做了架构:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.books.org"
        xmlns="http://www.books.org"
        elementFormDefault="qualified">
<xsd:element name="BookStore">
    <xsd:complexType>
        <xsd:sequence>
        <xsd:element name="Book" maxOccurs="unbounded">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="Status">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:pattern value="Available|Not available">
                                </xsd:pattern>
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="Title">
                        <xsd:complexType>
                            <xsd:attribute name="en" type="xsd:string" use="required"/> 
                        </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="Author" minOccurs="1" maxOccurs="unbounded">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name="Name">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:string">
                                            <xsd:pattern value="[a-zA-Z]+\\s[a-zA-Z]+">
                                            </xsd:pattern>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                                <xsd:element name="Genre">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:string">
                                            <xsd:enumeration value="M"/>
                                            <xsd:enumeration value="F"/>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                            </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="Date">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name="Day">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:positiveInteger">
                                            <xsd:minInclusive value="1"/>
                                            <xsd:maxInclusive value="31"/>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                                <xsd:element name="Month">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:positiveInteger">
                                            <xsd:minInclusive value="1"/>
                                            <xsd:maxInclusive value="12"/>
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                                <xsd:element name="Year">
                                    <xsd:simpleType>
                                        <xsd:restriction base="xsd:positiveInteger">
                                        </xsd:restriction>
                                    </xsd:simpleType>
                                </xsd:element>
                            </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="Price" type="xsd:decimal">                       
                    </xsd:element>
                    <xsd:element name="ISBN">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}|\d{1}-\d{3}-\d{5}-\d{1}|\d{1}-\d{2}-\d{6}-\d{1}">
                                </xsd:pattern>
                            </xsd:restriction>
                        </xsd:simpleType>                   
                    </xsd:element>
                    <xsd:element name="Publisher" type="xsd:string">
                    </xsd:element>
                </xsd:sequence>
                <xsd:attribute name="category" type="xsd:string" use="required"/>
            </xsd:complexType>
        </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

当我尝试在线验证时,我收到以下错误: http://www.utilities-online.info/xsdvalidation/?save=72595340-b1e9-4061-a655-c6cfb9cdac44-xsdvalidation#.UsivXPQW1PI    单击按钮验证xml对xsd并查看所有错误。有没有人知道解决这个问题没有任何错误?

1 个答案:

答案 0 :(得分:1)

现在问题的更改似乎已停顿,此处更新了XSD,以消除验证给定XML文档实例时收到的验证错误。

修正了XSD:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.books.org"
            xmlns="http://www.books.org"
            elementFormDefault="qualified">
  <xsd:element name="BookStore">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Book" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Status">
                <xsd:simpleType>
                  <xsd:restriction base="xsd:string">
                    <xsd:pattern value="Available|Not available">
                    </xsd:pattern>
                  </xsd:restriction>
                </xsd:simpleType>
              </xsd:element>
              <xsd:element name="Title">
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                      <xsd:attribute name="lang" type="xsd:string"/> 
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="Author" minOccurs="1" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="Name">
                      <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                          <xsd:pattern value="[a-zA-Z]+\s[a-zA-Z]+">
                          </xsd:pattern>
                        </xsd:restriction>
                      </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="Genre">
                      <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                          <xsd:enumeration value="M"/>
                          <xsd:enumeration value="F"/>
                        </xsd:restriction>
                      </xsd:simpleType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="Date">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="Day">
                      <xsd:simpleType>
                        <xsd:restriction base="xsd:positiveInteger">
                          <xsd:minInclusive value="1"/>
                          <xsd:maxInclusive value="31"/>
                        </xsd:restriction>
                      </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="Month">
                      <xsd:simpleType>
                        <xsd:restriction base="xsd:positiveInteger">
                          <xsd:minInclusive value="1"/>
                          <xsd:maxInclusive value="12"/>
                        </xsd:restriction>
                      </xsd:simpleType>
                    </xsd:element>
                    <xsd:element name="Year">
                      <xsd:simpleType>
                        <xsd:restriction base="xsd:positiveInteger">
                        </xsd:restriction>
                      </xsd:simpleType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="Price">                       
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:decimal">
                      <xsd:attribute name="type" type="xsd:string"/> 
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="ISBN">
                <xsd:simpleType>
                  <xsd:restriction base="xsd:string">
                    <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}|\d{1}-\d{3}-\d{5}-\d{1}|\d{1}-\d{2}-\d{6}-\d{1}|\d{1}-\d{4}-\d{5}-\d{1}">
                    </xsd:pattern>
                  </xsd:restriction>
                </xsd:simpleType>                   
              </xsd:element>
              <xsd:element name="Publisher" type="xsd:string">
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="category" type="xsd:string" use="required"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

次要说明:我在ISBN模式中添加了另一个表单,以支持上一本书的ISBN:8-4322-62332-6。如果这不是有效的ISBN格式,则应删除我添加的模式中的最后| - 子句(\d{1}-\d{4}-\d{5}-\d{1}),而是在XML文档实例中修复ISBN。