0

I am learning to work with XML schemas. I want to create an XML file based on the "address.xsd" schema file :

"address.xsd"

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema 
    elementFormDefault="qualified" 
    targetNamespace="com.namespace.address" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="AddressDetails">
        <xs:sequence>
            <xs:element name="building" type="xs:string" />
            <xs:element name="street" type="xs:string" />
            <xs:element name="city" type="xs:string" />
            <xs:element name="country" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

address.xml

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

    <a:AddressDetails 
       xmlns:a="com.namespace.address" 
       xsi:schemaLocation="D:/Prac/XML/address.xsd"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >    
        <a:building>Crosswords</a:building>
        <a:street>MainStreet</a:street>
        <a:city>LA</a:city>
        <a:country>USA</a:country>    
    </a:AddressDetails>

Iam not getting why this is not working.

Ash Ash
  • 443
  • 4
  • 7
  • 15
  • How do you know it's not working? In other words, what error message(s) are you getting? – Brian Driscoll Apr 04 '12 at 13:06
  • I am using an XML editor "Liquid XML Studio" for creating both the XSD as well as the XML file. In the XML file, the editor is giving the following error message: "The 'com.namespace.address:AddressDetails' element is not declared." – Ash Ash Apr 04 '12 at 13:14

2 Answers2

0

Actually XSD is used to validate xml not for XML generation

BALASCJP
  • 579
  • 2
  • 10
  • 21
  • yes, ,that is what i am trying to do. I want to validate "address.xml" against "address.xsd". In other words, i want to write an XML file that follows the definitions of the "address.xsd". – Ash Ash Apr 04 '12 at 13:12
  • @AshAsh you put **I want to create an XML file??** that's why i am confusing hope it helps http://stackoverflow.com/questions/15732/whats-the-best-way-to-validate-an-xml-file-against-an-xsd-file – BALASCJP Apr 04 '12 at 13:19
  • Sorry, if i have not framed the question properly. I am new to this XML schema subject. What i wanted to ask is, i am given an XML schema and want to write an XML file based on that schema. – Ash Ash Apr 04 '12 at 13:25
  • @AshAsh no problem link is useful for you – BALASCJP Apr 04 '12 at 13:28
0

Ok, i have figured out the problem. There was some problem with the namespaces. Here i am posting the "address.xsd" schema file along with the valid "address.xml".

address.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" targetNamespace="com.namespace.address" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="AddressDetails">
        <xs:sequence>
            <xs:element name="building" type="xs:string" />
            <xs:element name="street" type="xs:string" />
            <xs:element name="city" type="xs:string" />
            <xs:element name="country" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:element name="address" xmlns:q1="com.namespace.address" type="q1:AddressDetails" />
</xs:schema>

address.xml

<?xml version="1.0" encoding="utf-8"?>            
<a:address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="com.namespace.address address.xsd" xmlns:a="com.namespace.address">
    <a:building>Crosswords</a:building>
    <a:street>Main Street</a:street>
    <a:city>LA</a:city>
    <a:country>USA</a:country>   
</a:address>
Ash Ash
  • 443
  • 4
  • 7
  • 15