如何将名称空间添加到XSD? [英] How do I add namespaces to an XSD?

查看:86
本文介绍了如何将名称空间添加到XSD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的XML及其关联的XSD:

The following is my XML and its associated XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- <!DOCTYPE people SYSTEM "validator.dtd"> -->

<people xmlns:xsi="http://www.w3c.org/200/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="student.xsd">
    <student>
        <name>John</name>
        <course>Computer Technology</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>

    <student>
        <name>Foo</name>
        <course>Industrial Electronics</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>
</people>  

XSD

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="people">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="student" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string" />
                            <xs:element name="course" type="xs:string" />
                            <xs:element name="semester">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="1" />
                                        <xs:enumeration value="2" />
                                        <xs:enumeration value="3" />
                                        <xs:enumeration value="4" />
                                        <xs:enumeration value="5" />
                                        <xs:enumeration value="6" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="scheme">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value = "E|C" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>  


我现在想做的是添加一个名称空间-学生所属的一所假设大学-例如,卡内基·梅隆(Carnegie Mellon).


What I now want to do is to add a namespace - a hypothetical university to which the students belong - say, Carnegie Mellon.

我知道如何向XML文档添加名称空间.如下所示:
xmlns:cmu = "http://www.carnegiemellon.com/ns/students"
并且XML中将有关联的前缀.

I know how to add namespaces to an XML document. That would be as follows:
xmlns:cmu = "http://www.carnegiemellon.com/ns/students"
and there would be associated prefixes in the XML.

我想知道的是:如何使用XSD验证带有前缀的XML?

推荐答案

我的回答

My answer here should answer your question... Still, you have to consider the following:

人们和学生在同一个命名空间中吗?如果是,则只需添加一个您提到的uri的targetNamespace即可.否则,您必须添加xsd:import,并使用所需的targetNamespace创建一个新的XSD,以定义学生.

Are the people and student in the same namespace? if yes, then just add a targetNamespace with the uri that you mentioned. Otherwise, you have to add an xsd:import, and create a new XSD with the targetNamespace that you want which defines the student.

这篇关于如何将名称空间添加到XSD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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