如何修复xsd架构验证错误?

时间:2016-07-11 11:09:43

标签: xml xsd

我正在尝试根据MSDN中的示例编写XSD架构。我修改了示例,以便XSD是xml文件的本地,并且不使用命名空间。但是,XML无法验证。

XSD文件(sys_params.xsd):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://tempuri.org/po.xsd" 
xmlns="http://tempuri.org/po.xsd" elementFormDefault="qualified">
 <xs:annotation>
  <xs:documentation xml:lang="en">
   Purchase order schema for Example.com.
   Copyright 2000 Example.com. All rights reserved.
  </xs:documentation>
 </xs:annotation>

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

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

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

 <xs:complexType name="USAddress">
      <xs:annotation>
      <xs:documentation>
       Purchase order schema for Example.Microsoft.com.
       Copyright 2001 Example.Microsoft.com. All rights reserved.
      </xs:documentation>
      <xs:appinfo>
        Application info.
      </xs:appinfo>
     </xs:annotation>

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

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

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

</xs:schema>

XML文件(sys_params.xml):

<?xml version="1.0" encoding="UTF-8"?>

<purchaseOrder xmlns="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sys_params.xsd" orderDate="1999-10-20">
    <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>

xml文件的第3行无法验证:

<purchaseOrder xmlns="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="leda_sys_params.xsd" orderDate="1999-10-20">

错误是:

Error prefix 'xsi' can not be resolved to namespace URI

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

您不能直接在<xsd:element>下添加<xsd:complexType>

假设您想要第一个<shipTo>代码,然后是<item>代码,您将使用<xsd:sequence>收集代码来构成order类型的内容:

    <xsd:complexType name="order">
      <xsd:sequence>
        <xsd:element   name="shipTos"    type="shipAddress"/>
        <xsd:element   name="items"     type="cdItems"/>
      </xsd:sequence>
    </xsd:complexType>

完全修复的架构版本将转为:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xsd:element name="root"/>
    <xsd:element name="shipOrder" type="order"/>
    <xsd:complexType name="order">
        <xsd:sequence>
            <xsd:element name="shipTo" type="shipAddress"/>
            <xsd:element name="items" type="cdItems"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="shipAddress">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="street" type="xsd:string"/>
            <xsd:element name="address" type="xsd:string"/>
            <xsd:element name="country" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="cdItems">
        <xsd:sequence>
            <xsd:element name="item" minOccurs="0" maxOccurs="unbounded" type="cdItem"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="cdItem">
        <xsd:sequence>
            <xsd:element name="title" type="xsd:string"/>
            <xsd:element name="quantity" type="xsd:positiveInteger"/>
            <xsd:element name="price" type="xsd:decimal"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

注意:您尚未将<root>元素绑定到任何类型的内容...

答案 1 :(得分:0)

根据http://www.w3schools.com/xml/el_complextype.asp,复杂类型定义为

<complexType
id=ID
name=NCName
abstract=true|false
mixed=true|false
block=(#all|list of (extension|restriction))
final=(#all|list of (extension|restriction))
any attributes
>

(annotation?,(simpleContent|complexContent|((group|all|choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?))))

</complexType> 

这意味着xsd:element不是xsd:complexType的有效子元素。您必须使用xsd:groupxsd:allxsd:choicexsd:sequence代替。

E.g:

<xsd:complexType name="order">
    <xsd:sequence>
      <xsd:element   name="shipTos"    type="shipAddress"/>
      <xsd:element   name="items"     type="cdItems"/>
    </xsd:sequence>
</xsd:complexType>

答案 2 :(得分:0)

元素周围缺少xsd:sequence元素。

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

    <xsd:element name="root"/>
    <xsd:element name="shipOrder" type="order"/>

    <xsd:complexType name="order">
        <xsd:sequence>
            <xsd:element   name="shipTos"    type="shipAddress"/>
            <xsd:element   name="items"     type="cdItems"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="shipAddress">
        <xsd:sequence>
            <xsd:element   name="name"      type="xsd:string"/>
            <xsd:element   name="street"    type="xsd:string"/>
            <xsd:element   name="address"   type="xsd:string"/>
            <xsd:element   name="country"   type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="cdItems">
        <xsd:sequence>
            <xsd:element   name="item"      minOccurs="0" maxOccurs="unbounded" type="cdItem"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="cdItem">
        <xsd:sequence>
            <xsd:element   name="title"     type="xsd:string"/>
            <xsd:element   name="quantity"  type="xsd:positiveInteger"/>
            <xsd:element   name="price"     type="xsd:decimal"/>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>