首页 文章

为什么引导中的XML Schema示例无法验证?

提问于
浏览
1

我在XML Schema primerpo.xml 示例:

<?xml version="1.0"?>
<purchaseOrder orderDate="1999-10-20" xmlns="http://www.example.com/PO">
   <shipTo country="US">
      <name>Alice Smith</name>
      <street>123 Maple Street</street>
      <city>Mill Valley</city>
      <state>CA</state>
      <zip>90952</zip>
   </shipTo>
   <billTo country="US">
      <name>Robert Smith</name>
      <street>8 Oak Avenue</street>
      <city>Old Town</city>
      <state>PA</state>
      <zip>95819</zip>
   </billTo>
   <comment>Hurry, my lawn is going wild!</comment>
   <items>
      <item partNum="872-AA">
         <productName>Lawnmower</productName>
         <quantity>1</quantity>
         <USPrice>148.95</USPrice>
         <comment>Confirm this is electric</comment>
      </item>
      <item partNum="926-AA">
         <productName>Baby Monitor</productName>
         <quantity>1</quantity>
         <USPrice>39.98</USPrice>
         <shipDate>1999-05-21</shipDate>
      </item>
   </items>
</purchaseOrder>

当我验证它here时,它无法使用以下架构进行验证:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:po="http://www.example.com/PO" targetNamespace="http://www.example.com/PO">

  <xsd:annotation>
    <xsd:documentation xml:lang="en">
     Purchase order schema for Example.com.
     Copyright 2000 Example.com. All rights reserved.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>

  <xsd:element name="comment" type="xsd:string"/>

  <xsd:complexType name="PurchaseOrderType">
    <xsd:sequence>
      <xsd:element name="shipTo" type="USAddress"/>
      <xsd:element name="billTo" type="USAddress"/>
      <xsd:element ref="comment" minOccurs="0"/>
      <xsd:element name="items"  type="Items"/>
    </xsd:sequence>
    <xsd:attribute name="orderDate" type="xsd:date"/>
  </xsd:complexType>

  <xsd:complexType name="USAddress">
    <xsd:sequence>
      <xsd:element name="name"   type="xsd:string"/>
      <xsd:element name="street" type="xsd:string"/>
      <xsd:element name="city"   type="xsd:string"/>
      <xsd:element name="state"  type="xsd:string"/>
      <xsd:element name="zip"    type="xsd:decimal"/>
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN"
                   fixed="US"/>
  </xsd:complexType>

  <xsd:complexType name="Items">
    <xsd:sequence>
      <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="productName" type="xsd:string"/>
            <xsd:element name="quantity">
              <xsd:simpleType>
                <xsd:restriction base="xsd:positiveInteger">
                  <xsd:maxExclusive value="100"/>
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:element>
            <xsd:element name="USPrice"  type="xsd:decimal"/>
            <xsd:element ref="comment"   minOccurs="0"/>
            <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
          </xsd:sequence>
          <xsd:attribute name="partNum" type="SKU" use="required"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>

  <!-- Stock Keeping Unit, a code for identifying products -->
  <xsd:simpleType name="SKU">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="\d{3}-[A-Z]{2}"/>
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>

出现以下错误:

Src-resolve.4.1:解析组件'PurchaseOrderType'时出错 . 检测到'PurchaseOrderType'没有命名空间,但没有目标命名空间的组件不能从架构文档'null'引用 . 如果'PurchaseOrderType'意图具有命名空间,则可能需要提供前缀 . 如果意图'PurchaseOrderType'没有命名空间,那么没有“命名空间”属性的'import'应该被添加到'null' .

如果我运行 xmllint --schema po.xsd po.xml ,会发生类似情况:

po.xsd:10:元素元素:模式解析器错误:元素'{http://www.w3.org/2001/XMLSchema}element',属性'type':从此模式到无命名空间中的组件的引用不是允许,因为没有进口声明表明 .

(我将 xmlns:po="http://www.example.com/PO" targetNamespace="http://www.example.com/PO" 添加到在primer中找到的架构中的根元素,希望它能使它工作,但没有运气 . )

1 回答

  • 2

    为了清楚起见,您使用的架构和实例文档是XSD Primer中修改的变体 . 您已通过添加命名空间修改了它们,并且您已经错误地完成了操作 . 例如,您已将PurchaseOrderType更改为名称空间,但您尚未更改对PurchaseOrderType的引用以反映该更改;它需要像

    <xsd:element name="purchaseOrder" type="po:PurchaseOrderType"/>
    

    其中名称空间前缀po绑定到模式的目标名称空间 . 这同样适用于模式中的其他组件引用 .

相关问题