对于通过XSD验证的XML,是否需要/应该明确定义所有元素类型?

时间:2012-11-04 20:54:36

标签: xml xsd

对于通过XSD验证的XML文档,是否需要显式定义所有XML元素类型?

以下XSD没有BookTypeAuthorType但没有BookstoreType,但是还没有验证XML。定义BookstoreType而不是一般改进吗?

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="Bookstore">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="Book" type="BookType"
                         minOccurs="0" maxOccurs="unbounded" />
            <xsd:element name="Author" type="AuthorType"
                         minOccurs="0" maxOccurs="unbounded" />
         </xsd:sequence>
      </xsd:complexType>
      <xsd:key name="BookKey">
         <xsd:selector xpath="Book" />
         <xsd:field xpath="@ISBN" />
      </xsd:key>
      <xsd:key name="AuthorKey">
         <xsd:selector xpath="Author" />
         <xsd:field xpath="@Ident" />
      </xsd:key>
      <xsd:keyref name="AuthorKeyRef" refer="AuthorKey">
         <xsd:selector xpath="Book/Authors/Auth" />
         <xsd:field xpath="@authIdent" />
      </xsd:keyref>
      <xsd:keyref name="BookKeyRef" refer="BookKey">
         <xsd:selector xpath="Book/Remark/BookRef" />
         <xsd:field xpath="@book" />
      </xsd:keyref>
   </xsd:element>
   <xsd:complexType name="BookType">
      <xsd:sequence>
         <xsd:element name="Title" type="xsd:string" />
         <xsd:element name="Authors">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:element name="Auth" maxOccurs="unbounded">
                     <xsd:complexType>
                        <xsd:attribute name="authIdent" type="xsd:string"
                                       use="required" />
                     </xsd:complexType>
                  </xsd:element>
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
         <xsd:element name="Remark" minOccurs="0">
            <xsd:complexType mixed="true">
               <xsd:sequence>
                  <xsd:element name="BookRef" minOccurs="0"
                               maxOccurs="unbounded">
                     <xsd:complexType>
                        <xsd:attribute name="book" type="xsd:string"
                                       use="required" />
                     </xsd:complexType>
                  </xsd:element>
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="ISBN" type="xsd:string" use="required" />
      <xsd:attribute name="Price" type="xsd:integer" use="required" />
   </xsd:complexType>
   <xsd:complexType name="AuthorType">
      <xsd:sequence>
         <xsd:element name="First_Name" type="xsd:string" />
         <xsd:element name="Last_Name" type="xsd:string" />
      </xsd:sequence>
      <xsd:attribute name="Ident" type="xsd:string" use="required" />
   </xsd:complexType>
</xsd:schema>

验证XML文档

<?xml version="1.0" ?>
<Bookstore>
   <Book ISBN="ISBN-0-13-713526-2" Price="100">
      <Title>A First Course in Database Systems</Title>
      <Authors>
         <Auth authIdent="JU" />
         <Auth authIdent="JW" />
      </Authors>
   </Book>
   <Book ISBN="ISBN-0-13-815504-6" Price="85">
      <Title>Database Systems: The Complete Book</Title>
      <Authors>
         <Auth authIdent="HG" />
         <Auth authIdent="JU" />
         <Auth authIdent="JW" />
      </Authors>
      <Remark>
        Amazon.com says: Buy this book bundled with
        <BookRef book="ISBN-0-13-713526-2" /> - a great deal!
      </Remark>
   </Book>
   <Author Ident="HG">
      <First_Name>Hector</First_Name>
      <Last_Name>Garcia-Molina</Last_Name>
   </Author>
   <Author Ident="JU">
      <First_Name>Jeffrey</First_Name>
      <Last_Name>Ullman</Last_Name>
   </Author>
   <Author Ident="JW">
      <First_Name>Jennifer</First_Name>
      <Last_Name>Widom</Last_Name>
   </Author>
</Bookstore>

如果定义BookstoreType,XSD会如何?

1 个答案:

答案 0 :(得分:1)

您已将Bookstore的类型定义为匿名类型(嵌套在Bookstore元素声明中的ComplexType元素)。这与使用命名类型(就像你为其他元素所做的那样)完全相同 - 唯一的实际区别是匿名类型不可重用或可重构。