Walt
Walt

Reputation: 45

Referencing namespaces in XSD

Need to get an XSD definition that can generate output like this. I'm having trouble referencing namespaces. I want to do it all in one XSD file (do not want to reference other files).

<?xml version="1.0" encoding="UTF-8"?>
<message>
  <Headerinfo>
    <Element1>stuff</Element1>
    <Element2>things</Element2>
  </Headerinfo>
  <CreateRequest xmlns="http://schemas.datacontract.org/2004/07/MyOrg.MyApp.DataContracts.MyObject" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Element3>more stuff</Element3>
    <Element4>more things</Element4>
    <Address xmlns:a="http://schemas.datacontract.org/2004/07/MyOrg.MyApp.DataContracts.Address">
      <a:AddressLine1>123 Main Street</a:AddressLine1>
    </Address>
  </CreateRequest>
</message>

1 - Need to be able to generate the xmlns for the CreateRequest complex element.
2 - Need to be able to generate the xmlns:a for Address and its simple elements.

Here is the XSD I have so far. I believe I need to add an xs:scehma entry for the a: record. Not sure what else, can't seem to get it working.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="message">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Headerinfo">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Element1" type="xs:string"/>
              <xs:element name="Element2" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="CreateRequest">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Element3" type="xs:string"/>
              <xs:element name="Element4" type="xs:string"/>
              <xs:element name="Address">
                <xs:complexType>
                  <xs:sequence>
                    <a:element name="AddressLine1" type="xs:string"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Upvotes: 2

Views: 253

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

I'm not sure what you mean by XSD "generating output" - XSD is usually used for validating input.

But the XML Schema spec requires you to write one schema document per namespace, with each document containing xs:import declarations to define the dependencies across namespaces. That's the way it's designed and there's no point trying to fight it.

Upvotes: 1

Petru Gardea
Petru Gardea

Reputation: 21638

It is not possible to achieve this without referencing another XSD file, since the root has no namespace, and some content uses namespaces. In fact, you need exactly three XSD files.

In general, the other way around might be possible, by setting the form to unqualified for elements without a namespace.

Upvotes: 1

Related Questions