XSD使用同一文件中定义的复杂类型

时间:2015-12-09 14:15:13

标签: xml xsd

我有以下XSD文件(相关位):

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://somenamespace"
        elementFormDefault="qualified">

    <complexType name="UserType">
        <sequence>
            <element name="id" type="int" minOccurs="0" maxOccurs="1"/>
            <element name="registrationDate" type="date" minOccurs="0" maxOccurs="1"/>
            <element name="email" type="string" minOccurs="0" maxOccurs="1"/>
            <element name="name" type="string" minOccurs="0" maxOccurs="1"/>
            <element name="surname" type="string" minOccurs="0" maxOccurs="1"/>
        </sequence>
    </complexType>

    <complexType name="UserListType">
        <sequence>
            <element name="userList" type="tns:UserType" minOccurs="0" maxOccurs="unbounded"/>
        </sequence>
    </complexType>

</schema>

我遇到的问题是这是无效的。当我尝试在线验证XSD时,我收到此错误:Cannot resolve tns:UserType as a QName, the prefix tns is not declared

据我所知,tns引用了“this namespace”,我需要在这里使用它,因为我已经将全局命名空间定义为架构标记中的XMLSchema。它必须是一些小的疏忽,我对此很新。

1 个答案:

答案 0 :(得分:2)

AFAIK在w3c架构中没有任何预定义的tns命名空间,因此您必须将xmlns:tns="http://somenamespace"添加到架构标记中。