1

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

下面的 XSD 没有 aBookType和 aAuthorType但没有BookstoreType,但仍然可以很好地验证 XML。改为定义 a 通常会是一种改进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>

如果也定义了 a,XSD 会是什么样子BookstoreType

4

1 回答 1

1

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

于 2012-11-05T09:30:16.800 回答