XML XSD名称空间问题

时间:2014-09-11 15:46:12

标签: xml xsd xml-namespaces

我正在尝试使用银行帐户的示例定义一个简单的XML和XSD文件。

这是我的XSD定义我的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
xmlns:bank="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified"
targetNamespace="http://www.auto-owners.com/accounts"
>

    <xs:element name="accounts" >
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" ref="account"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="account" type="account">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="uuid"/>
                <xs:element ref="name"/>
                <xs:element ref="balance"/>
                <xs:element ref="status"/>
                <xs:element ref="opened"/>
                <xs:element ref="closed"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="uuid" type="xs:string"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="balance" type="xs:decimal"/>
    <xs:element name="status" type="xs:NCName"/>
    <xs:element name="opened" type="xs:string"/>
    <xs:element name="closed" type="xs:string"/>
</xs:schema>

这是我使用上面的XSD文件的XML文件:

<?xml version="1.1" encoding="UTF-8"?>
<bank:accounts
    xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.auto-owners.com/Account account.xsd"
    xmlns:bank="http://www.auto-owners.com/Account"
    >

    <bank:account>
        <bank:uuid>19cab0a2-c44b-4f3e-b24e-5f21dd23c7e8
</bank:uuid>
        <bank:name>Bob Dylan</bank:name>
        <bank:balance>1233.12</bank:balance>
        <bank:status>active</bank:status>
        <bank:opened>01/02/2000</bank:opened>
        <bank:closed></bank:closed>
    </bank:account>
    <bank:account>
        <bank:uuid>2e2142a2-1c36-4604-9bfd-f0681b89f775
</bank:uuid>
        <bank:name>Bonnie Tyler</bank:name>
        <bank:balance>34566.21</bank:balance>
        <bank:status>active</bank:status>
        <bank:opened>03/02/2000</bank:opened>
        <bank:closed></bank:closed>
    </bank:account>
    <bank:account>
        <bank:uuid>216361b1-0bd5-455d-b6a0-400f92f61d68
</bank:uuid>
        <bank:name>Dolly Parton</bank:name>
        <bank:balance>9876.32</bank:balance>
        <bank:status>active</bank:status>
        <bank:opened>01/05/2011</bank:opened>
        <bank:closed></bank:closed>
    </bank:account>
    <bank:account>
        <bank:uuid>140ee47a-d323-448d-a5e6-db2454a16934
</bank:uuid>
        <bank:name>Gary Moore</bank:name>
        <bank:balance>8764.12</bank:balance>
        <bank:status>hold</bank:status>
        <bank:opened>01/22/2010</bank:opened>
        <bank:closed></bank:closed>
    </bank:account>
    </bank:accounts>

我一直在尝试定义要使用的命名空间,每次我尝试绑定到bank时:命名空间我得到一组循环错误。我现在在:

  

解析组件“帐户”时出错。检测到'account'没有命名空间,但没有目标命名空间的组件是            无法从架构文档'file:///.../account.xsd'引用。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

以下是您需要更改的内容:

  1. 在你的XSD中,如Jim Garrison所述, xmlns:bank="http://www.w3.org/2001/XMLSchema"错了;这应该 是xmlns:bank="http://www.auto-owners.com/accounts"
  2. 在您的XSD中,当您引用类型或元素时,请在其前面添加 目标命名空间的名称空间前缀。
  3. 在您的XML中,xsi:schemaLocation="http://www.auto-owners.com/Account account.xsd"应该是 xsi:schemaLocation="http://www.auto-owners.com/accounts account.xsd";案件事项和必须小心谨慎 命名空间字面意思。
  4. 更正的XSD如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema 
        xmlns:bank="http://www.auto-owners.com/accounts"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        elementFormDefault="qualified"
        targetNamespace="http://www.auto-owners.com/accounts">
    
      <xs:element name="accounts">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="bank:account"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="account">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="bank:uuid"/>
            <xs:element ref="bank:name"/>
            <xs:element ref="bank:balance"/>
            <xs:element ref="bank:status"/>
            <xs:element ref="bank:opened"/>
            <xs:element ref="bank:closed"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="uuid" type="xs:string"/>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="balance" type="xs:decimal"/>
      <xs:element name="status" type="xs:NCName"/>
      <xs:element name="opened" type="xs:string"/>
      <xs:element name="closed" type="xs:string"/>
    </xs:schema>
    

    更正的XML如下:

    <?xml version="1.1" encoding="UTF-8"?>
    <bank:accounts
        xmlns="http://www.w3schools.com"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.auto-owners.com/accounts account.xsd"
        xmlns:bank="http://www.auto-owners.com/accounts">    
      <bank:account>
        <bank:uuid>19cab0a2-c44b-4f3e-b24e-5f21dd23c7e8
        </bank:uuid>
        <bank:name>Bob Dylan</bank:name>
        <bank:balance>1233.12</bank:balance>
        <bank:status>active</bank:status>
        <bank:opened>01/02/2000</bank:opened>
        <bank:closed></bank:closed>
      </bank:account>
      <bank:account>
        <bank:uuid>2e2142a2-1c36-4604-9bfd-f0681b89f775
        </bank:uuid>
        <bank:name>Bonnie Tyler</bank:name>
        <bank:balance>34566.21</bank:balance>
        <bank:status>active</bank:status>
        <bank:opened>03/02/2000</bank:opened>
        <bank:closed></bank:closed>
      </bank:account>
      <bank:account>
        <bank:uuid>216361b1-0bd5-455d-b6a0-400f92f61d68
        </bank:uuid>
        <bank:name>Dolly Parton</bank:name>
        <bank:balance>9876.32</bank:balance>
        <bank:status>active</bank:status>
        <bank:opened>01/05/2011</bank:opened>
        <bank:closed></bank:closed>
      </bank:account>
      <bank:account>
        <bank:uuid>140ee47a-d323-448d-a5e6-db2454a16934
        </bank:uuid>
        <bank:name>Gary Moore</bank:name>
        <bank:balance>8764.12</bank:balance>
        <bank:status>hold</bank:status>
        <bank:opened>01/22/2010</bank:opened>
        <bank:closed></bank:closed>
      </bank:account>
    </bank:accounts>
    

    以这种方式使用命名空间,XML现在将针对XSD进行验证。