xml - why can't I have an xs:element using ref as a global element in a XSD? -


i experimenting ref attributes in <xsd:element> , don't following:

while <xsd:element> ref attribute can defined in non-global scope (i.e. not directly below <schema>), in:

ref1.xsd

<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.dummy-example.com" xmlns:foo="http://www.dummy-example.com">   <xs:element name="a" type ="xs:string"/>  <xs:element name="b">     <xs:complextype>         <xs:sequence>             <xs:element ref="foo:a"/>         </xs:sequence>     </xs:complextype> </xs:element> </xs:schema> 

so that, e.g. following validates xmllint:

ref1.xml

<?xml version="1.0"?> <foo:b xmlns:foo="http://www.dummy-example.com">     <foo:a>whatever ...</foo:a> </foo:b> 

however, cannot have referencing element directly @ global level. e.g. following:

ref2.xsd

<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.dummy-example.com" xmlns:foo="http://www.dummy-example.com">     <xs:element name="a" type ="xs:string"/>     <xs:element ref="foo:a"/> </xs:schema> 

does not validate ref2.xml below:

ref2.xml

<?xml version="1.0"?> <foo:a xmlns:foo="http://www.dummy-example.com">     whatever  </foo:a>    

in fact xmlint complains during parsing of xsd file, before reaching xml file:

ref2.xsd:6: element element: schemas parser error : element '{http://www.w3.org/2001/xmlschema}element': attribute 'name' required missing. 

update

following accepted answer, found constraint spelled out in xml schema primer:

one caveat global declarations cannot contain references; global declarations must identify simple , complex types directly.

what expect top level ref provide top level declaration doesn't give you? if

<xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.dummy-example.com" xmlns:foo="http://www.dummy-example.com">     <xs:element name="a" type ="xs:string"/>     <xs:element ref="foo:a"/> </xs:schema> 

were allowed, "a document conforming schema can have top level element named a in http://www.dummy-example.com namespace, or alternatively top level element named a in http://www.dummy-example.com namespace" - add nothing beyond simply

<xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" targetnamespace="http://www.dummy-example.com" xmlns:foo="http://www.dummy-example.com">     <xs:element name="a" type ="xs:string"/> </xs:schema> 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -