I have an XSD that defines an complex type and has the targetNamespace
attribute set. Is it correct that the TestElement
will not gain the namespace set by targetNamespace
? It should gain the namespace from the complex type afn:ElementType
and therefore http://anotherfancy.namespace
, right?
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sfn="http://somefancy.namespace"
xmlns:afn="http://anotherfancy.namespace"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://somefancy.namespace"
version="1.0">
<xs:import namespace="http://anotherfancy.namespace" schemaLocation="..."/>
<xs:element name="MyComplexType">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="TestElement" type="afn:ElementType">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xs:schema/elementFormDefault="qualified"
(As in your case, which is also the recommended and most commonly used setting of elementFormDefault
.)
The elements declared within an XSD must be in the namespace given by the XSD's targetNamespace
.
Therefore, for your XSD, TestElement
will have to be in the http://somefancy.namespace
for the XML document to be valid. If you instead want it to be in the http://anotherfancy.namespace
, declare the element in the imported XSD; storing its type there will not place the element itself in that other namespace. Once TestElement
is declared in the imported namespace, it can be used in the original namespace via xs:element/@ref
.
See also How to reference element in other XSD's namespace?
See Michael Kay's answer here and my longer answer to this question: What does elementFormDefault do in XSD?