Marcus
Marcus

Reputation: 627

How to write xsd file with multiple namespace in XML?

When i define XML schema in the mec.xsd it doesn't work for the element. How can i resolve this? Thanks.

<l:primary>XML</l:primary>

mec.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.example.com mc.xsd"
         xmlns:l="http://www.example2.com"
         xmlns="http://www.example.com"> 
    <person>
        <name>Marcus</name>
        <language>
            <l:primary>XML</l:primary>
        </language>
    </person>
</people>

mc.xsd

<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.example.com"
           xmlns="http://www.example.com"
           elementFormDefault="qualified">
    <xs:element name="people">
        <xs:complexType mixed="true">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="person">
                    <xs:complexType mixed="true">
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="language">
                                <xs:complexType mixed="true">
                                    <xs:element name="primary" type="xs:string"/>            
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

Upvotes: 9

Views: 13424

Answers (1)

Kachna
Kachna

Reputation: 2961

  • You have to use two schemas. one schema per namespace.
  • you have to use xsd:import to bring in an XSD from a different namespace.

  • You have to Validate the xml document using only the main schema (mc.xsd).

primary.xsd (imported schema)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
    targetNamespace="http://www.example2.com"> 
    <xs:element name="primary" type="xs:string"/>
</xs:schema>

mc.xsd (main schema)

<xs:schema version="1.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com"
    xmlns="http://www.example2.com"
    elementFormDefault="qualified">
    <xs:import namespace="http://www.example2.com" schemaLocation="primary.xsd"/>
    <xs:element name="people">
        <xs:complexType mixed="true">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="person">
                    <xs:complexType mixed="true">
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="language">
                                <xs:complexType mixed="true">
                                    <xs:sequence>
                                        <xs:element ref="primary"/>   
                                    </xs:sequence>

                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

Upvotes: 14

Related Questions