Page 1 of 1

XSD for > 1 alternative root element?

Posted: Thu Jul 06, 2017 5:29 pm
by mhGLEIF
Hi all,

I've come across this supposed possibility for enabling validation of 2 different XML documents using 1 schema:

https://stackoverflow.com/questions/116 ... -at-a-time

Is this a documented feature of XML schema?

Re: XSD for > 1 alternative root element?

Posted: Thu Jul 06, 2017 5:38 pm
by mhGLEIF
I verified in oxygen that this type of schema does in fact validate alternate root xml instances.

I discovered it also seems to imply that XSDs which use the "ref" attribute to define child elements also end up allowing those elements as possible root elements e.g.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="b">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="child" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="child" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="child">
    <xs:complexType />
  </xs:element>
</xs:schema>


It allows roots `<a>`, `<b>` but also `<child>`...

This is based on a schema auto-generated by oxygen BTW so maybe you want to think if this is the best way to generate these?

Re: XSD for > 1 alternative root element?

Posted: Thu Jul 06, 2017 5:58 pm
by adrian
Hi,

Any global element (declared at top level) of the schema can be used as root. The XML validation simply checks that all components adhere to the schema, starting from whatever root the XML provides.


Oxygen uses Trang for generating XML schema from XML instances. Trang prefers to declare the elements as global, rather than making them local.
This makes it easy to reuse/rewrite the generated schema.
Sorry, what's the question here? Are you asking why the generated schema allows multiple possible roots?

Regards,
Adrian