Re: XSD validation, ambiguous root XML instance element

Basically, the problem is that XSD doesn't define constraints on a document, it only defines constraints on an element. I've never understood entirely why that mind-set exists, but it seems to be strongly held by some.

xs:ID/IDREF validation is of course an exception, and I think that only got in because they wanted XSD to be a functional replacement for DTDs.

Michael Kay
Saxonica

> On 31 Oct 2020, at 11:48, Mukul Gandhi <gandhi.mukul@gmail.com> wrote:
> 
> Hi all,
>    Please look at following XSD validation use case, and my doubts mentioned after that.
> 
> My XML instance document is following (that I wish to validate with XSD),
> 
> x.xml
> 
> <?xml version="1.0"?>
> <X>
>   <a>
>     <p/>
>   </a>
>   <a>
>     <q/>
>   </a>
> </X>
> 
> I'm validating the above cited XML instance document, with following XSD document,
> 
> x.xsd
> 
> <?xml version="1.0"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema <http://www.w3.org/2001/XMLSchema>">
>      
>      <xs:include schemaLocation="x1.xsd"/>
>      
>      <xs:element name="X">
>        <xs:complexType>
>          <xs:sequence>
>            <xs:element name="a" maxOccurs="unbounded">
>               <xs:complexType>
>                  <xs:sequence>
>                     <xs:any/>
>                  </xs:sequence>
>               </xs:complexType>
>            </xs:element>
>          </xs:sequence>
>        </xs:complexType>
>      </xs:element>
> 
> </xs:schema>
> 
> Following is XSD document x1.xsd, that's included by above cited XSD document,
> 
> <?xml version="1.0"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema <http://www.w3.org/2001/XMLSchema>">
>     
>    <xs:element name="p"/>
>    
>    <xs:element name="q"/>
> 		
> </xs:schema>
> 
> When I do an XSD validation as stated above, the XML validation outcome is reported as valid. This is ok with me.
> 
> Below are my doubts and concerns,
> 
> Due to an xs:include that I'm using, within the XSD document x.xsd, the valid XML instance root elements can be X, p or q. But, I wish that, the XML schema for my example above, should prohibit the XML instances root elements p or q. I wish that, only an XML instance element X should be a valid XML instance root. Is this achievable, with the current XSD 1.0 or 1.1 languages? If not, I'd like to suggest following changes to my XSD document x.xsd,
> 
> <?xml version="1.0"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema <http://www.w3.org/2001/XMLSchema>">
>      
>      <xs:include schemaLocation="x1.xsd"/>
>      
>      <xs:root>
>         <xs:assert test="not(self::p | self::q)"/>
>      </xs:root>
>      
>      <xs:element name="X">
>        <xs:complexType>
>          <xs:sequence>
>            <xs:element name="a" maxOccurs="unbounded">
>               <xs:complexType>
>                  <xs:sequence>
>                     <xs:any/>
>                  </xs:sequence>
>               </xs:complexType>
>            </xs:element>
>          </xs:sequence>
>        </xs:complexType>
>      </xs:element>
> 
> </xs:schema>
> 
> i.e, I wish the following change to XSD language,
> We should be able to specify an optional xs:root element as child of xs:schema element, that can mention XML schema validation constraints for the XML instance root element. With the above XSD document example, xs:root element specifies that, XML elements p and q cannot be the root XML elements of the XML document that's validated.
> 
> Any thoughts please?           
>            
> 
> 
> -- 
> Regards,
> Mukul Gandhi

Received on Saturday, 31 October 2020 14:02:08 UTC