XML XSD namespace issues

2019-07-02 05:41发布

问题:

I am trying to define a simple XML and XSD file using the example of bank accounts.

Here is my XSD defining my XML file:

<?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>

And here is my XML file using the XSD file above:

<?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>

I have been running around trying to define the namespace to use and every time I try to bind to the bank: namespace I get a revolving set of errors. I am currently on:

Error resolving component 'account'. It was detected that 'account' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///.../account.xsd'.

Any ideas?

回答1:

Here's what you have to change:

  1. In your XSD, as noted by Jim Garrison, xmlns:bank="http://www.w3.org/2001/XMLSchema" is wrong; it should be xmlns:bank="http://www.auto-owners.com/accounts".
  2. In your XSD, when you reference a type or element, prefix it with the namespace prefix of the target namespace.
  3. In your XML, xsi:schemaLocation="http://www.auto-owners.com/Account account.xsd" should be xsi:schemaLocation="http://www.auto-owners.com/accounts account.xsd"; case matters and care must be taken to match the namespace literally.

The corrected XSD is as follows:

<?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>

The corrected XML is as follows:

<?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>

Using namespaces in this manner, the XML will now validate against the XSD.