Jonas WS
Jonas WS

Reputation: 95

XSD expects right element of wrong namespsace

I want to define a schema in namespace "foo" that imports a schema in namespace "bar" with complex types defined and makes references to types in "bar". What am I missing to make this validate? MWE below.

Root schema:

<?xml version="1.0" encoding="ISO-8859-1"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:f="foo" xmlns:b="bar" targetNamespace="foo">

<import schemaLocation="Import.xsd" namespace="bar"/>

<element name="root" type="f:Root"/>

<complexType name="Root">
    <sequence>
        <!--<element ref="b:imported"/>-->
        <element name="imported" type="b:ImportedType"/>
    </sequence>
</complexType>
</schema>

Imported schema:

<?xml version="1.0" encoding="ISO-8859-1"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="bar"
    elementFormDefault="qualified">
<complexType name="ImportedType"/>
<!--
<element name="imported">
    <complexType/>
</element>
-->
</schema>

XML instance:

<?xml version="1.0" encoding="UTF-8"?>
<f:root xmlns:f="foo" xmlns:b="bar">
    <b:imported/>
</f:root>

Result: Element '{bar}imported': This element is not expected. Expected is ( {foo}imported ).

If I change design pattern from Venetian Blind to Salami Slice (toggle comments in schemas) everything works. But all our other schemas are in VB so I would prefer not to change for this case.

Tried to validate with both xmllint and notepad++

Upvotes: 1

Views: 49

Answers (1)

lexicore
lexicore

Reputation: 43709

You expected to have b:imported instead of f:imported.

The thing is, you have imported and used a type. And your type ImportedType is still in b.

Your element imported is, however (despite what the name says) not imported from b, but declared in f.

Therefore f:imported is correct and expected.

If you want to "escape" the namespace with the element, declare imported in f and use an element reference instead:

<xs:element ref="b:imported"/>

Upvotes: 1

Related Questions