如何在XML中编写带有多个命名空间的xsd文件?

时间:2015-10-08 17:14:16

标签: xml xsd

当我在mec.xsd中定义XML模式时,它不适用于该元素。我该如何解决这个问题?感谢。

<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>

1 个答案:

答案 0 :(得分:3)

  • 您必须使用两个架构。每个命名空间一个模式。
  • 您必须使用xsd:import从其他人那里引入XSD 命名空间。

  • 您必须仅使用主模式验证xml文档 (的 mc.xsd )。

primary.xsd (导入架构)

<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 (主要架构)

<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>