views:

230

answers:

1

When trying to import shared definitions from a XML Schema, I can properly reference shared types, but referencing shared elements causes validation errors.

This is the schema that imports the shared definitions (example.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
  elementFormDefault="qualified"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:shared="http://shared.com"&gt;

    <xs:import namespace="http://shared.com" schemaLocation="shared.xsd"/>

    <xs:element name="example">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="importedElement"/>
                <xs:element ref="importedType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="importedElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="shared:fooElement"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="importedType">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="bar" type="shared:barType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

These are the shared definitions (shared.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://shared.com"
    targetNamespace="http://shared.com"&gt;

    <xs:element name="fooElement">
        <xs:simpleType>
            <xs:restriction base="xs:integer"/>
        </xs:simpleType>
    </xs:element>

    <xs:simpleType name="barType">
        <xs:restriction base="xs:integer"/>
    </xs:simpleType>

</xs:schema>

Now consider this XML instance:

<?xml version="1.0"?>
<example
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                     
  xsi:noNamespaceSchemaLocation="example.xsd">
    <importedElement>
        <fooElement>42</fooElement>
    </importedElement>
    <importedType>
        <bar>42</bar>
    </importedType>
</example>

When validated, the "importedType" works perfectly fine, but the "importedElement" gives the following error:

Invalid content was found starting with element 'fooElement'. One of '{"http://shared.com":fooElement}' is expected

I would guess that my troubles are related to namespace issues (hence the somehow misleading "got fooElement but was expecting fooElement") -- any hints on what's wrong here?

A: 

You are referencing fooElement as if it was in no namespace, you need to use the correct namespace in your instance document:

<?xml version="1.0"?>
<example
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                     
  xsi:noNamespaceSchemaLocation="example.xsd" xmlns:shared="http://shared.com"&gt;
    <importedElement>
        <shared:fooElement>42</shared:fooElement>
    </importedElement>
    <importedType>
        <bar>42</bar>
    </importedType>
</example>

Edit: I should have pointed out: that's the differences between types and elements; only the latter appear in documents (with some exceptions), that's why your imported type works as you wanted, and your element does not.

xcut
I've accepted your answer because it addresses my question. Unfortunately, it doesn't solve my problem: The XML instance documents are deployed at the customer and it is not an option to change them. Any namespace-magic that would allow importing of elements from a shared XSD *not* requiring adjustments in the XML instances?
netzwerg
Well, what made you assign a namespace to the shared schema in the first place? Is there some sort of mandate for that? Why don't you make your shared schema have no namespace like your mail, and use include instead of import?
xcut
We're using JAXB to access the XML documents later. With imports, the Java counterparts of the shared types are not truly shared, but re-generated for every XSD, making it difficult to write utility code around them.
netzwerg
I guess you mean "with includes" there, since your current proposed schema uses imports? :) With an include, you could specify in your binding file that all classes from the shared (included) schema end up in a certain package. So, yes, they will be overwritten if another schema uses the shared elements, but they'll remain the same classes. Would that work for you?
xcut
Yeah, that could work, I will give it a try -- thanks for your help!
netzwerg