XSD for > 1 alternative root element?

This should cover W3C XML Schema, Relax NG and DTD related problems.
mhGLEIF
Posts: 43
Joined: Tue Jul 26, 2016 6:31 pm

XSD for > 1 alternative root element?

Post 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?
mhGLEIF
Posts: 43
Joined: Tue Jul 26, 2016 6:31 pm

Re: XSD for > 1 alternative root element?

Post 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?
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: XSD for > 1 alternative root element?

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