Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import XSD in Another XSD

Tags:

java

xsd

I am attempting to import xsd to another xsd. i am seeing some problems to import. i could not understand the solutions which provided in web. below is my XSD.

I have HEADER.xsd. it is common for all other xsd's.

HEADER.XSD

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="Header" type="reqHeader"/>

  <xs:complexType name="reqHeader">
    <xs:sequence>
      <xs:element name="MsgId" type="xs:string" minOccurs="0"/>
      <xs:element name="MsgDesc" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

MESSAGE1.XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:import namespace="" schemaLocation="\resources\xsd\HEADER.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" />

  <xs:element name="Message">
    <xs:complexType>
      <xs:sequence>

        <xs:element name="Header" type="xs:reqHeader" />

        <xs:element name="Body">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="User">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="Name" minOccurs="1"/>
                    <xs:element type="xs:int" name="DOB" minOccurs="1"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Here i am trying to import element called since it is common for all xsd's [MESSAGE1.XSD MESSAGE2.XSD and so on].

Exception says : Is not a valid:src-resolve.4.2: Error resolving component 'xs:reqHeader'. It was detected that 'xs:reqHeader' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:/D:/Projects/workspace/Message/resources/xsd/MESSAGE1.xsd'. If this is the incorrect namespace, perhaps the prefix of 'xs:reqHeader' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:/D:/Projects/workspace/Message/resources/xsd/MESSAGE1.XSD'.

My Project structure is :

/src/com

/lib

/resources/xsd/MESSAGE1.XSD

Please someone help me to resolve it.

like image 941
deadend Avatar asked Feb 05 '23 08:02

deadend


1 Answers

Bingo! After fighting a lot with the XSDs I have found an error free way:

Here's the code:

  • You need to change the xs:import to xs:include
  • Remove the namespace="" and the xmlns:xs="http://www.w3.org/2001/XMLSchema" from the xs:include line

MESSAGE1.xsd

<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:include schemaLocation="header.xsd" />

<xs:element name="Message">
    <xs:complexType>
        <xs:sequence>

            <xs:element name="Header" type="reqHeader" />

            <xs:element name="Body">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="User">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element type="xs:string" name="Name" minOccurs="1" />
                                    <xs:element type="xs:int" name="DOB" minOccurs="1" />
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

HEADER.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Header" type="reqHeader" />

<xs:complexType name="reqHeader">
    <xs:sequence>
        <xs:element name="MsgId" type="xs:string" minOccurs="0" />
        <xs:element name="MsgDesc" type="xs:string" minOccurs="0" />
    </xs:sequence>
</xs:complexType>

Please modify the XSD location according to your local file's location.

Hope it helps!

like image 167
Sourav Purakayastha Avatar answered Feb 19 '23 10:02

Sourav Purakayastha