Restricting complex imported type

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

Restricting complex imported type

Sun Jul 04, 2010 11:11 pm

Hi all,

I somehow don't manage to restrict an imported complex type: I keep getting a "derivation-ok-restriction.5.4.2: Error for type "RestrictedMapData. The particle of the type is not a valid restriction of the particle of the base." error on the following code.
Any ideas?

(The imported schema E142 does not restrict derivation; if I put the same derivation type into the original file, everything is fine...)

Thanks for your help! Stefan

Code: Select all

<?xml version="1.0" ?>
<xs:schema id="MapData" targetNamespace="urn:semi-org:xsd.E142-1.V0805.SubstrateMap_ACMEProductsInc"
  xmlns:sm="urn:semi-org:xsd.E142-1.V0805.SubstrateMap" xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="urn:semi-org:xsd.E142-1.V0805.SubstrateMap_ACMEProductsInc" elementFormDefault="qualified">

  <xs:import namespace="urn:semi-org:xsd.E142-1.V0805.SubstrateMap"
    schemaLocation="E142-1-V0805-Schema.xsd"/>

  <xs:complexType name="RestrictedMapData">
    <xs:complexContent>
      <xs:restriction base="sm:MapDataType">
        <xs:sequence>
          <xs:element minOccurs="0" name="Substrates" type="sm:SubstratesType"/>
          <xs:element minOccurs="0" name="SubstrateMaps" type="sm:SubstrateMapsType"/>
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>
adrian
Posts: 2388

Re: Restricting complex imported type

Mon Jul 05, 2010 3:55 pm

Hello,

I'm afraid that you can't do this kind of restriction in the main schema because you have a different target namespace than in the imported sschema and the elements from the restriction are from the main schema's namespace so they are not equivalent to the ones from the imported schema.

A quick solution here is to create another schema that includes E142-1-V0805-Schema.xsd and has the same namespace:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:semi-org:xsd.E142-1.V0805.SubstrateMap"
    xmlns:sm="urn:semi-org:xsd.E142-1.V0805.SubstrateMap">
   
    <xs:include schemaLocation="E142-1-V0805-Schema.xsd"/>
   
    <xs:complexType name="RestrictedMapData">
        <xs:complexContent>
            <xs:restriction base="sm:MapDataType">
                <xs:sequence>
                    <xs:element minOccurs="0" name="Substrates" type="sm:SubstratesType"/>
                    <xs:element minOccurs="0" name="SubstrateMaps" type="sm:SubstrateMapsType"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>


Next you can import this in the main schema instead of E142-1-V0805-Schema.xsd and it should work the way you want it to.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Stefan_E
Posts: 18

Re: Restricting complex imported type

Mon Jul 05, 2010 10:03 pm

Thanks, Adrian.

Unfortunately, I get exactly the same error message on your code.

Stefan
george
Site Admin
Posts: 2100

Re: Restricting complex imported type

Mon Jul 05, 2010 11:12 pm

Hi,

The algorithm for checking restriction is specified in the XML Schema specification and there are cases when that rejects restrictions that intuitively seem correct. Please try to get a reduced but fully functional example that reproduce the issue and post that here or send it to our support@oxygenxml.com address so we can have a look at that.

Best Regards,
George
George Cristian Bina
Stefan_E
Posts: 18

Re: Restricting complex imported type

Mon Jul 05, 2010 11:31 pm

Hi George,

thanks - a brutal strip-down of E142 is here:

Code: Select all

<?xml version="1.0" ?>
<xs:schema id="MapData" targetNamespace="urn:semi-org:xsd.E142-1.V0805.SubstrateMap"
   xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:semi-org:xsd.E142-1.V0805.SubstrateMap"
   elementFormDefault="qualified">
   <xs:annotation>
      <xs:appinfo>Schema for SEMI E142 Substrate Mapping</xs:appinfo>
   </xs:annotation>
   <!-- Elements -->
   <xs:element name="MapData" type="MapDataType"> </xs:element>
   <!-- Complex Types -->
   <xs:complexType name="MapDataType">
      <xs:sequence>
         <xs:element name="Layouts" minOccurs="0"/>
         <xs:element name="Substrates" minOccurs="0"/>
         <xs:element name="SubstrateMaps" minOccurs="0"/>
      </xs:sequence>
   </xs:complexType>
   <!-- Simple Types -->
</xs:schema>


That means I had to slightly modify my original code, but still get the same error:

Code: Select all

<?xml version="1.0" ?>
<xs:schema id="MapData" targetNamespace="urn:semi-org:xsd.E142-1.V0805.SubstrateMap_ACMEProductsInc"
    xmlns:sm="urn:semi-org:xsd.E142-1.V0805.SubstrateMap"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:semi-org:xsd.E142-1.V0805.SubstrateMap_ACMEProductsInc"
    elementFormDefault="qualified">

    <xs:import namespace="urn:semi-org:xsd.E142-1.V0805.SubstrateMap"
        schemaLocation="E142-1-V0805-Schema.xsd"/>

    <xs:complexType name="RestrictedMapData">
        <xs:complexContent>
            <xs:restriction base="sm:MapDataType">
                <xs:sequence>
                    <xs:element minOccurs="0" name="Substrates"/>
                    <xs:element minOccurs="0" name="SubstrateMaps"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>


The full E142 schema is available here as ZIP

Stefan
george
Site Admin
Posts: 2100

Re: Restricting complex imported type

Tue Jul 06, 2010 10:05 am

Dear Stefan,

The sample files are enough to understand the issue. When you define an element inside a complex type that defines a local element in the schema. That element is placed either in the target namespace of the schema or in no namespace, depending on the value of the form attribute. In your case you defined elementFormDefault as qualified and that means that all local declarations are placed in the target namespace of the schema.

Now, when the restriction is checked by the parser it will match sequence from base with the sequence from the restricted type then it tries to match the Substrates element from the restricted type with a Substrates element from base. But an element is identified both by its local name and namespace and as you set elementsFormDefault to qualified the namespace is different, thus they do not match.

See also http://www.w3.org/TR/xmlschema-1/#rcase-NameAndTypeOK. That describes what checks are performed to match two element declarations, see in particular the first point:
1 The declarations' {name}s and {target namespace}s are the same.

If you change the elementsFormDefault attribute to unqualified in both schema and thus placing the local elements in no namespace then the Substrates and SubstrateMaps will match as in both schemas they have the same namespace.
Also, as you observed, if you move the restriction in the same schema as the base type then that also will work, because again the local elements will get the same namespace.

There is no possibility to declare the local elements in other namespace than the target namespace or no namespace in an XML Schema file (in Relax NG it is possible to declare elements in any namespace in any schema).

Hope this clarifies things for you. If you have any further questions do not hesitate to get in touch again.

Best Regards,
George
George Cristian Bina
adrian
Posts: 2388

Re: Restricting complex imported type

Tue Jul 06, 2010 10:28 am

Hi,

If you need to keep elementFormDefault="qualified" in E142 you can still use the include trick I've shown you, you just have to specify the same elementFormDefault="qualified" attribute on the root of that schema.

Code: Select all

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:semi-org:xsd.E142-1.V0805.SubstrateMap"
    xmlns:sm="urn:semi-org:xsd.E142-1.V0805.SubstrateMap" elementFormDefault="qualified">
   
    <xs:include schemaLocation="E142-1-V0805-Schema.xsd"/>
...


Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Stefan_E
Posts: 18

Re: Restricting complex imported type

Wed Jul 07, 2010 9:41 pm

Hi George, Adrian,

of course, both of you are correct. I still don't 100% understand what is going on, but at least I now know where to dig - so after another couple of reads, I'll get my head around that...

How does one actually learn this stuff :?: ? As a novice, I find myself spoiled with the graphical schema editing, but the books I've seen so far refer to 'only' the textual representation of a schema. Now, I don't blame anybody who says 'such is live' :) but I find that this discrepancy makes the learning curve rather steep ... - any hints? (Currently, I have 'The XML Schema Companion' by Neil Bradley on my desk, but have still refused to read it front to back... :oops: )

Thanks anyway so far - and I'll annoy with next questions as they come along :roll:

Stefan
george
Site Admin
Posts: 2100

Re: Restricting complex imported type

Thu Jul 08, 2010 12:21 pm

There are always complains that XML Schema is complex.

For some resources please check our XML books section
http://www.oxygenxml.com/xml_books.html

I can recommend also Roger Costello tutorials on different XML technologies - many of them contain instructions on how to use/configure oXygen for that technology:
http://www.xfront.com/

For XML Schema see
http://www.xfront.com/files/xml-schema.html

Best Regards,
George
George Cristian Bina
Stefan_E
Posts: 18

Re: Restricting complex imported type

Thu Jul 08, 2010 11:53 pm

George,

thanks a lot - great stuff from Roger!

Stefan

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 2 guests