Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write xsd file with multiple namespace in XML?

Tags:

xml

xsd

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>
like image 308
Marcus Avatar asked Oct 08 '15 17:10

Marcus


People also ask

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

How do I give a namespace in XSD?

In writing XSD schemas, you can use the XSD targetNamespace attribute to specify a target namespace. This topic describes how the XSD targetNamespace, elementFormDefault, and attributeFormDefault attributes work, how they affect the XML instance that is generated, and how XPath queries are specified with namespaces.

Can XSD have multiple root elements?

While a properly formed XML file can only have a single root element, an XSD or DTD file can contain multiple roots.

Can XML have multiple schemas?

Schemas can be composed of one or more XML documents. These schema documents can be explicitly joined together using the include and import elements.


1 Answers

  • 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>
like image 93
Kachna Avatar answered Sep 30 '22 06:09

Kachna