XML Schema: Namespace issues when importing shared elements
Posted
by netzwerg
on Stack Overflow
See other posts from Stack Overflow
or by netzwerg
Published on 2010-04-14T12:01:33Z
Indexed on
2010/04/14
12:03 UTC
Read the original article
Hit count: 1041
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">
<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">
<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?
© Stack Overflow or respective owner