XML架构XSD文件错误

时间:2012-11-02 12:50:05

标签: xml xsd schema

我有以下示例XML,我想根据以下架构进行验证。我收到有关额外图像的错误。错误是:

  

错误3033:元素'{http://www.w3.org/2001/XMLSchema}sequence':The   内容无效。预期是(注释?,(元素|组|   选择|序列|有)*)。

谁能看到我做错了什么?当我在单独的模式中使用extraImages复杂类型时,它可以工作,但是当我将它添加到产品模式时不起作用。

<products>
<product>
<productImageURL>imgp9241.jpg</productImageURL>
<productDescription>blah blah blah</productDescription>
<productName>Test Extra</productName>
<extraImages>
<extraImages>
<fileName>textextra.jpg</fileName>
<sequence>10</sequence>
</extraImages>
<extraImages>
<fileName>textextra.jpg</fileName>
<sequence>10</sequence>
</extraImages>
</extraImages>
</product>
</products>

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="products">
   <xsd:complexType>
      <xsd:sequence>
         <xsd:element name="product" minOccurs = "1" maxOccurs="1" type="productType"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:element>

    <xsd:complexType name="productType">
       <xsd:sequence>

           <xsd:element name="productImageURL">
              <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="0" />
                    <xsd:maxLength value="450" />
                    <xsd:whiteSpace value="collapse" /> 
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:element>

           <xsd:element name="productDescription">
              <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="1"/>
                    <xsd:whiteSpace value="collapse"/> 
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:element>

           <xsd:element name="productName">
              <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="1"/>
                    <xsd:maxLength value="150"/>
                    <xsd:whiteSpace value="collapse"/> 
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:element>


            <xsd:element name="extraImages">
               <xsd:complexType>
                  <xsd:sequence>
                     <xsd:element name="extraImage" minOccurs = "1" maxOccurs="100" type="imgType"/>
                  </xsd:sequence>
               </xsd:complexType>
            </xsd:element>

                <xsd:complexType name="imgType">
                   <xsd:sequence>

                         <xsd:element name="fileName">
                          <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="25"/>
                                <xsd:whiteSpace value="collapse"/> 
                            </xsd:restriction>
                          </xsd:simpleType>
                        </xsd:element>

                        <xsd:element name="sequence" type="xsd:integer" default="0"/>

                   </xsd:sequence>
                </xsd:complexType>          


       </xsd:sequence>
    </xsd:complexType>  

</xsd:schema>

2 个答案:

答案 0 :(得分:4)

错误消息告诉您 XSD 无效。您不能将命名的类型嵌套在除架构重新定义之外的任何其他内容中。

更正后的XSD:

<?xml version="1.0"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="products">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="product" minOccurs = "1" maxOccurs="1" type="productType"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="productType">
        <xsd:sequence>
            <xsd:element name="productImageURL">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:minLength value="0"/>
                        <xsd:maxLength value="450"/>
                        <xsd:whiteSpace value="collapse"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="productDescription">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:minLength value="1"/>
                        <xsd:whiteSpace value="collapse"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="productName">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:minLength value="1"/>
                        <xsd:maxLength value="150"/>
                        <xsd:whiteSpace value="collapse"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="extraImages">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="extraImage" minOccurs = "1" maxOccurs="100" type="imgType"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="imgType">
        <xsd:sequence>
            <xsd:element name="fileName">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:maxLength value="25"/>
                        <xsd:whiteSpace value="collapse"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="sequence" type="xsd:integer" default="0"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

它显示了这种布局:

enter image description here

修复XSD后,您可以尝试验证您显示的XML。在查看其结构时,将其与XSD进行比较......

enter image description here

您可以看到extraImages中的 extraImages 嵌套应该没有s

Error occurred while loading [], line 7 position 5
The element 'extraImages' has invalid child element 'extraImages'. List of possible elements expected: 'extraImage'.

答案 1 :(得分:0)

在XSD中,您使用了两个不同的元素: extraImages extraImage
但是在您的XML中,您使用 extraImages 两次而不是每次使用extraImages和extraImage