0

I receive this kind of XML:

<?xml version="1.0" encoding="utf-8"?>
<root-element xmlns="SomeSpecificNameSpace">
    <some-elements />
    <some-other-elements/>
    <some-other-other-element/>
    <element-with-empty-namespace xmlns=""/>
</root-element>

I must make a check that I've this element-with-empty-namespace, but that I don't care about the content, so I tried the following:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema          targetNamespace="SomeSpecificNameSpace"
                    elementFormDefault="qualified"
                    xmlns="SomeSpecificNameSpace"
                    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="MyRootElement">
        <xs:complexType>
            <xs:sequence>
            <!-- Others types here -->
            </xs:sequence>
        </xs:complexType>           
    </xs:element>
    <xs:element name="element-with-empty-namespace" minOccurs="0" maxOccurs="1" xmlns="">
        <xs:complexType>
            <xs:sequence>
                <xs:any minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:anyAttribute/>
        </xs:complexType>
    </xs:element>
</xs:schema

but it seems it doesn't accept my element, saying that it is expecting an element with the namespace SomeSpecificNameSpace.

I know that this structure is a little bit weird, but this element-with-empty-namespace is one "value" we provide through XML.

4
  • I believe that element-with-empty-namespace "inherits" SomeSpecificNamespace simply by being declared inside root-element
    – sblandin
    Jul 13, 2015 at 12:14
  • Yes, but my input xml is not with the same namespace everywhere
    – J4N
    Jul 13, 2015 at 12:21
  • The XML you need to process is exactly like the sample you provided? I am talking about the XML not the xsd.
    – sblandin
    Jul 13, 2015 at 12:59
  • Besides, the element-with-empty-namespace should be inside the MyRootElement which name should be root-namespace according the xml sample.
    – sblandin
    Jul 13, 2015 at 13:26

1 Answer 1

2

Multiple namespaces require multiple XSDs.

To support your XML input structure,

<?xml version="1.0" encoding="utf-8"?>
<root-element xmlns="SomeSpecificNameSpace"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="SomeSpecificNameSpace try.xsd">
  <some-elements />
  <some-other-elements/>
  <some-other-other-element/>
  <element-with-empty-namespace xmlns=""/>
</root-element>

break your XSD into two files as follows...

In the main XSD file, which governs the SomeSpecificNameSpace namespace, use xs:import,

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="SomeSpecificNameSpace"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:import schemaLocation="element-with-empty-namespace.xsd"/>

  <xs:element name="root-element">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="some-elements"/>
        <xs:element name="some-other-elements"/>
        <xs:element name="some-other-other-element"/>
        <xs:element ref="element-with-empty-namespace"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

and import a second XSD,

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="element-with-empty-namespace">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
      <xs:anyAttribute/>
    </xs:complexType>
  </xs:element>
</xs:schema>

that houses the element-with-empty-namespace element in no namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.