Alex Rivera | Logout

how to handle multiple namespaces with different URI in XSD

Asked 2012-03-21T13:13:40.283
9

I have an XML (first.xml) which looks like ::

 <?xml version="1.0" encoding="utf-8"?>
 <saw:jobInfo xmlns:saw="com.analytics.web/report/v1.1">     
      <saw:jobStats>...........</saw:jobStats>    
       <saw:detailedInfo> .....</saw:detailedInfo>    
       <saw:fileInfo>..........</saw:fileInfo>
 </saw:jobInfo> 

The below XML (second.xml) is same as the above but with a different namespace.

<?xml version="1.0" encoding="utf-8"?> 
 <soap:jobInfo xmlns:soap="urn://bi.webservices/v6">  
   <soap:jobStats>...........</saw:jobStats>
   <soap:detailedInfo> .....</saw:detailedInfo>    
   <soap:fileInfo>..........</saw:fileInfo>
 </soap:jobInfo> 

As I have the same element and attribute names in both the xml's I am using the same xsd file to validate both.

XSD file ::

 <?xml version="1.0" encoding="utf-8" ?>  
 <xs:schema targetNamespace="com.analytics.web/report/v1.1"      
  xmlns="com.analytics.web/report/v1.1" 
  xmlns:saw="com.analytics.web/report/v1.1" 
  xmlns:soap="urn://bi.webservices/v6"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"> 

After including xmlns:soap="urn://bi.webservices/v6" the schema validation failed for second.xml saying unkown element "soap:jobinfo". I poked around and found the targetNamespace value should be same as the namespace URI. Please let me know how to use the same XSD for two different namespaces having different URIs.

Edit
Report

1 Answer

17

Short answer is you can't. You could, however, if you would be using three XSDs. It would allow you to have all the XSD that matters into one file (Chameleon.XSD), and have two more that simply compose Chameleon.XSD, those two having the namespaces you want.

Chameleon.XSD

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="jobInfo">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="jobStats" type="xsd:string" />
        <xsd:element name="detailedInfo" type="xsd:string" />
        <xsd:element name="fileInfo" type="xsd:string" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

JobInfo1.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema xmlns="com.analytics.web/report/v1.1" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="com.analytics.web/report/v1.1" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:include schemaLocation="Chameleon.xsd"/>
</xsd:schema>

JobInfo2.xsd

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema xmlns="urn://bi.webservices/v6" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn://bi.webservices/v6" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:include schemaLocation="Chameleon.xsd"/>
</xsd:schema>

Relationships:

answered 2012-03-21T13:22:37.753

Your Answer