JoséNunoFerreira
JoséNunoFerreira

Reputation: 170

How to use a namespace defined type from one XSD in another? (referencing it by namespace)

DISCLAIMER: I am not too well versed in XSD, and may be expressing myself incorrectly.

I have an XML file I need to validate and handle. I can change the XSDs but not the XML.

The XML file is of type a.xsd, and a.xsd includes a type from b.xsd. This type is called in the XML referring the namespace, like so:

<StuffDtl>
<elem1>test string</elem1>
<elem2>9</elem2>
<supertype:uuuh_special>test string</supertype:uuuh_special>
</StuffDtl>

The XSDs are defined like below:

a.xsd

<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:supertype="http://www.b.com">
<xs:import  namespace="http://www.b.com"
schemaLocation="b.xsd" />
<xs:element name="StuffDtl">
<xs:complexType>
<xs:sequence>
<xs:element name="elem1"         type="xs:decimal" />
<xs:element name="elem2"         type="xs:int" />
<xs:element name="uuuh_special"  type="supertype:SuperType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

b.xsd

<!-- language: lang-xml -->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.b.com"
elementFormDefault="qualified">
<xs:complexType name="SuperType">
<xs:sequence>
<xs:choice>
<xs:group ref="one_thing" />
<xs:group ref="another_thing" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:group name="one_thing">
<xs:sequence>
<xs:element name="year" type="xs:decimal" />
<xs:sequence minOccurs="0">
<xs:element name="hour" type="xs:decimal" />
</xs:sequence>
</xs:sequence>
</xs:group>
<xs:group name="another_thing">
<xs:sequence>
<xs:element name="date-value" type="xs:string" />
<xs:element name="date-format" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:group>
</xs:schema>

Upvotes: 0

Views: 53

Answers (1)

kjhughes
kjhughes

Reputation: 111716

Full stop if you truly cannot change the XML; it's not well-formed because a namespace prefix (supertype) cannot be used without being defined.

See Well-formed vs Valid XML for further details.

Assuming that you can change the XML to be well-formed, you might keep uuuh_special in a separate namespace,

<StuffDtl xmlns:supertype="http://www.b.com">
  <elem1>test string</elem1>
  <elem2>9</elem2>
  <supertype:uuuh_special>test string</supertype:uuuh_special>
</StuffDtl>

Or, you might take it out:

<StuffDtl>
  <elem1>test string</elem1>
  <elem2>9</elem2>
  <uuuh_special>test string</uuuh_special>
</StuffDtl>

Also, note that much of your b.xsd does not agree with the content of your uuh_special element in your XML. So, either much of the b.xsd is out of play, and uuh_special has to be redefined, or again, your XML will have to be modified.

There are so many loose ends and contradictions in the requirements at this point that this is about as much help as can be offered without excessive guessing.

Upvotes: 1

Related Questions