XSD for > 1 alternative root element?

This should cover W3C XML Schema, Relax NG and DTD related problems.
mhGLEIF
Posts: 40

XSD for > 1 alternative root element?

Thu Jul 06, 2017 5:29 pm

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?
mhGLEIF
Posts: 40

Re: XSD for > 1 alternative root element?

Thu Jul 06, 2017 5:38 pm

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?
adrian
Posts: 2326

Re: XSD for > 1 alternative root element?

Thu Jul 06, 2017 5:58 pm

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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 1 guest