我有一个XML文件和两个XSD文件。两个XSD文件都是格式良好并且有效。 XML文件格式正确,但当我尝试对其进行验证时,它会在第25行显示“#34;错误”,第46行:键入' http://www.w3.org/2001/XMLSchema:topicType'未找到。这是import语句的错误吗?导入的XSD文件的代码出错了,我声明了topicType? XML文件有问题吗?任何帮助将不胜感激,因为我无法解决错误。
XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<presentations
xmlns="http://www.example.com/contacts"
xmlns:name="http://www.example.com/name"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/contacts main.xsd"
source="Beginning XML 5E"
version="1.0">
<presentation date="2013-07-31" length="PT30M">
<topic genre="Music">PianoML</topic>
<presenters>
<name title="Mr." id="Y258">
<first>Elvis</first>
<middle>A</middle>
<last>Presley </last>
</name>
<name title="Miss" id="X365">
<first>Lady</first>
<last>Gaga</last>
</name>
</presenters>
</presentation>
<presentation date="2013-08-05" length="PT35M">
<topic genre="Science">AlienML</topic>
<presenters>
<name title="Mr." id="Y007">
<first>Will</first>
<last>Smith</last>
</name>
<name title="Mr." id="Y360">
<first>Tommy</first>
<first>Lee</first>
<last>Jones</last>
</name>
</presenters>
</presentation>
</presentations>
主XSD文件:
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:contacts="http://www.example.com/contacts"
xmlns:name="http://www.example.com/name"
targetNamespace="http://www.example.com/contacts"
elementFormDefault="qualified">
<import namespace="http://www.example.com/name" schemaLocation="topic.xsd" />
<!-- Presentations Element -->
<complexType name="presentationsType">
<sequence minOccurs="0" maxOccurs="unbounded">
<element ref="presentation"/>
</sequence>
</complexType>
<!-- Presentation Element -->
<complexType name="presentationType">
<sequence>
<element ref="topic"/>
<element ref="presenters"/>
</sequence>
<!-- Attribute group reference for presentation Element -->
<attributeGroup ref="presentationAttr" />
</complexType>
<!-- Presenters Element -->
<complexType name="presentersType">
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="name"/>
</sequence>
</complexType>
<!-- Name Element -->
<complexType name="nameType">
<!-- Element group reference for name Group -->
<group ref="nameGroup" />
<!-- Attribute group reference for name Element -->
<attributeGroup ref="nameAttr" />
</complexType>
<!-- Element declarations -->
<element name="presentations" type="presentationsType" />
<element name="presentation" type="presentationType" />
<element name="presenters" type="presentersType" />
<element name="name" type="nameType" />
<element name="first" type="string"/>
<element name="middle" type="string"/>
<element name="last" type="string"/>
<!--Element group definition for name Element -->
<group name="nameGroup">
<sequence>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="first"/>
</sequence>
<sequence minOccurs="0" maxOccurs="1">
<element ref="middle"/>
</sequence>
<element ref="last"/>
</sequence>
</group>
<!-- Attribute group definition for presentation Element -->
<attributeGroup name="presentationAttr">
<attribute name="date" type="date" use="required"/>
<attribute name="length" type="duration" use="required"/>
</attributeGroup>
<!-- Attribute group definition for name Element -->
<attributeGroup name="nameAttr">
<!-- ID must begin with either X or Y and be followed by 3 digits -->
<attribute name="id" use="required">
<simpleType>
<restriction base="ID">
<pattern value="[X|Y][0-9]{3}" />
</restriction>
</simpleType>
</attribute>
<!-- Title attribute must be either Mr., Mrs., Ms., or Miss -->
<attribute name="title" use="required">
<simpleType>
<restriction base="string">
<enumeration value="Mr."/>
<enumeration value="Mrs."/>
<enumeration value="Ms."/>
<enumeration value="Miss"/>
</restriction>
</simpleType>
</attribute>
</attributeGroup>
</schema>
导入的XSD文件:
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.example.com/name"
targetNamespace="http://www.example.com/name"
elementFormDefault="qualified">
<!-- Topic Element -->
<complexType name="topicType">
<simpleContent>
<extension base="string">
<attribute name="genre" use="required">
<simpleType>
<restriction base="string">
<enumeration value="ART" />
<enumeration value="Music" />
<enumeration value="Science" />
<enumeration value="Technology" />
</restriction>
</simpleType>
</attribute>
</extension>
</simpleContent>
</complexType>
<element name="topic" type="topicType" />
</schema>
答案 0 :(得分:1)
首先,您在主模式中使用default namespace
和target namespace
时遇到问题。默认名称空间是XML模式名称空间,这意味着所有非限定元素和类型引用都引用该名称空间。
但是,您在架构中定义的类型属于目标命名空间,当您引用它们时,它们必须是限定的,因为目标命名空间不是默认命名空间。为方便起见,通常应将默认命名空间与目标命名空间相同。但是,在这种情况下,让我们坚持你拥有的命名空间声明,这意味着你必须限定你的类型引用。一个例子:
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:contacts="http://www.example.com/contacts"
xmlns:name="http://www.example.com/name" targetNamespace="http://www.example.com/contacts"
elementFormDefault="qualified">
<import namespace="http://www.example.com/name" schemaLocation="topic.xsd" />
<!-- Presentations Element -->
<complexType name="presentationsType">
<sequence minOccurs="0" maxOccurs="unbounded">
<element ref="contacts:presentation" />
</sequence>
</complexType>
</schema>
请注意使用 contacts:presentation
而不是 presentation
。
其次,main.xsd
架构没有正确引用topic.xsd
中的类型。您必须再次使用名称空间前缀限定类型引用:
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:contacts="http://www.example.com/contacts"
xmlns:name="http://www.example.com/name" targetNamespace="http://www.example.com/contacts"
elementFormDefault="qualified">
...
<!-- Presentation Element -->
<complexType name="presentationType">
<sequence>
<element ref="name:topic" />
<element ref="contacts:presenters" />
</sequence>
<!-- Attribute group reference for presentation Element -->
<attributeGroup ref="contacts:presentationAttr" />
</complexType>
...
</schema>
请注意使用 name:topic
而不是 topic
。
第三,您必须限定XML实例文档中topic
元素的用法,因为该元素被定义为存在于name
命名空间中:
<?xml version="1.0" encoding="UTF-8"?>
<presentations
xmlns="http://www.example.com/contacts"
xmlns:name="http://www.example.com/name"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/contacts main.xsd" >
<presentation date="2013-07-31" length="PT30M">
<name:topic genre="Music">PianoML</name:topic>
<presenters>
<name title="Mr." id="Y258">
<first>Elvis</first>
<middle>A</middle>
<last>Presley </last>
</name>
<name title="Miss" id="X365">
<first>Lady</first>
<last>Gaga</last>
</name>
</presenters>
</presentation>
</presentations>
嗯,关于它,我想;)
更新使用&#34;推荐&#34;它会是什么样子?名称空间前缀的样式?
NB。这不是官方推荐的用法 - 只是一种常用的做法,可以更容易地引用本地类型定义。
以第二个模式为例:目标名称空间与默认名称空间相同:
<?xml version="1.0"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.com/name"
targetNamespace="http://www.example.com/name"
elementFormDefault="qualified">
<!-- Topic Element -->
<xsd:complexType name="topicType">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="genre" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="ART" />
<xsd:enumeration value="Music" />
<xsd:enumeration value="Science" />
<xsd:enumeration value="Technology" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:element name="topic" type="topicType" />
</xsd:schema>
现在,在元素topic
声明中,您可以参考topicType
而不限定它(我错过了在原始的anser中指出的内容,顺便说一句)。但请注意,您现在必须限定对内置架构架构类型的引用,例如xsd:string
。