Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle multiple namespaces with different URI in XSD

Tags:

xml

xsd

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.

like image 403
prathima Avatar asked Mar 21 '12 13:03

prathima


People also ask

How do you add multiple namespaces in xsd?

You have to use two schemas. one schema per namespace. you have to use xsd:import to bring in an XSD from a different namespace. You have to Validate the xml document using only the main schema (mc.

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

What is the use of TargetNamespace in xsd?

Figure 1: Elements and attributes in XML Schema namespace are used to write an XML Schema document, which generates elements and attributes as defined by user and puts them in {target namespace}. This {target namespace} is then used to validate the XML instance.

How do you define namespace prefix in xsd?

Each of the elements in the schema has a prefix xsd: which is associated with the XML Schema namespace through the declaration, xmlns:xsd="http://www.w3.org/2001/XMLSchema", that appears in the schema element. The prefix xsd: is used by convention to denote the XML Schema namespace, although any prefix can be used.


2 Answers

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:

Chameleon relationships

If you want one XSD to validate them all, then you can go and build a fourth one, that imports these two.

OneAll.XSD

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema xmlns="urn:tempuri-org:XSD:1" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:tempuri-org:XSD:1" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:import namespace="com.analytics.web/report/v1.1" schemaLocation="JobInfo1.xsd"/>
    <xsd:import namespace="urn://bi.webservices/v6" schemaLocation="JobInfo2.xsd"/> 
</xsd:schema>

Updated relationships:

One all

like image 111
Petru Gardea Avatar answered Sep 29 '22 19:09

Petru Gardea


The end tags in second.xml need to match the beginning rags (i.e. soap does not equal saw)

like image 22
Herb Miller Avatar answered Sep 29 '22 20:09

Herb Miller