XML XSD namespace issues
I am trying to define a simple XML and XSD file using a bank account example.
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'm trying to figure out the namespace to use, and every time I try to bind to the jar: namespace, I get a spinning set of errors. Now I am:
Error in defining account component. It was found that "account" does not have a namespace, but components without a target namespace do not reference the schema document file: ///.../account.xsd '.
Any ideas?
+3
source to share
1 answer
Here you must change:
- In your XSD, as Jim Garnison pointed out,
xmlns:bank="http://www.w3.org/2001/XMLSchema"
is wrong; should bexmlns:bank="http://www.auto-owners.com/accounts"
. - In your XSD, when you refer to a type or element, attach its target namespace namespace prefix.
- Your XML
xsi:schemaLocation="http://www.auto-owners.com/Account account.xsd"
should havexsi:schemaLocation="http://www.auto-owners.com/accounts account.xsd"
; and steps must also be taken to literally namespace.
The corrected XSD looks like this:
<?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 revised XML looks like this:
<?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>
By using namespaces in this way, XML will now be validated against XSD.
+3
source to share