如何在 XML 中编写具有多个命名空间的 xsd 文件? [英] How to write xsd file with multiple namespace in XML?

查看:29
本文介绍了如何在 XML 中编写具有多个命名空间的 xsd 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 mec.xsd 中定义 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>

推荐答案

  • 您必须使用两个架构.每个命名空间一个架构.
  • 您必须使用 xsd:import 从不同的命名空间.

    您必须仅使用主架构验证 xml 文档(mc.xsd).

    You have to Validate the xml document using only the main schema (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>
    

    这篇关于如何在 XML 中编写具有多个命名空间的 xsd 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆