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

How to write xsd file with multiple namespace in XML?

当我在 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>
  • 您必须使用两个架构。每个命名空间一个模式。
  • 你必须使用 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>