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 fo开发者_C百科oElement") -- any hints on what's wrong here?
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">
<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.
精彩评论