xmlxsdxmlspy

Adding namespace to single element doesn't conform to xsd


All I want to do is add a single namespace to a single element, S_NO, but I can't figure it out. Help would be appreciated. I keep getting errors when I add prefixes and stuff on the element. It won't recognise the element that is called within the XSD when using XMLSpy.

XML Code:

<MSG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/test_xml/izzypod5/test_input.xsd">
 <BODY>
   <bar:S_NO xmlns:bar="http://www.bar.org/">4423</bar:S_NO>
 </BODY>
</MSG>

XSD Code:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:bar="http://www.bar.org/">
<!-- Element Declarations -->
<xs:element name="S_NO" type="S_NOType" nillable="true">
    <xs:annotation>
        <xs:documentation>S_No; Mandatory Field</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="BODY" type="BODYType">
    <xs:annotation>
        <xs:documentation>Body</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="MSG" type="MSGType">
    <xs:annotation>
        <xs:documentation>Message encapsulates Body</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:simpleType name="S_NOType">
    <xs:restriction base="xs:string">
        <xs:minLength value="0"/>
        <xs:maxLength value="15"/>
    </xs:restriction>
</xs:simpleType>
<!-- Complex Type Declarations -->
<xs:complexType name="BODYType">
    <xs:sequence>
        <xs:element ref="S_NO"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="MSGType">
    <xs:sequence>
        <xs:element ref="BODY"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="REQUEST_MSGType">
    <xs:sequence>
        <xs:element ref="MSG"/>
    </xs:sequence>
</xs:complexType>
</xs:schema>

Solution

  • You have to use another XSD for the element you wish to have in a different namespace.

    XML document instance:

    <?xml version="1.0" encoding="UTF-8"?>
    <MSG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="file:///C:/test_xml/izzypod5/test_input.xsd"
         xmlns:bar="http://www.bar.org/">
      <BODY>
        <bar:S_NO xmlns:bar="http://www.bar.org/">4423</bar:S_NO>
      </BODY>
    </MSG>
    

    Main XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:bar="http://www.bar.org/">
    
      <xs:import namespace="http://www.bar.org/" schemaLocation="bar.xsd"/>
    
      <!-- Element Declarations -->
      <xs:element name="BODY" type="BODYType">
        <xs:annotation>
          <xs:documentation>Body</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element name="MSG" type="MSGType">
        <xs:annotation>
          <xs:documentation>Message encapsulates Body</xs:documentation>
        </xs:annotation>
      </xs:element>
      <!-- Complex Type Declarations -->
      <xs:complexType name="BODYType">
        <xs:sequence>
          <xs:element ref="bar:S_NO"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="MSGType">
        <xs:sequence>
          <xs:element ref="BODY"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="REQUEST_MSGType">
        <xs:sequence>
          <xs:element ref="MSG"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    

    Supporting XSD (bar.xsd) for S_NO:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:bar="http://www.bar.org/"
               targetNamespace="http://www.bar.org/">
      <xs:element name="S_NO" type="bar:S_NOType" nillable="true">
        <xs:annotation>
          <xs:documentation>S_No; Mandatory Field</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:simpleType name="S_NOType">
        <xs:restriction base="xs:string">
          <xs:minLength value="0"/>
          <xs:maxLength value="15"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:schema>