当使用Xerces和给定的XSD解析一些外来XML文件时,我收到一个错误,指出标记未声明。
标记声明如下:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://alicebot.org/2001/AIML-1.0.1" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sch="http://www.ascc.net/xml/schematron"
targetNamespace="http://alicebot.org/2001/AIML-1.0.1" elementFormDefault="qualified"
attributeFormDefault="unqualified" version="1.0" xml:lang="EN">
<xs:element name="aiml">
<xs:annotation>
<xs:documentation>An AIML object is represented by an aiml element in an XML document.</xs:documentation>
<xs:appinfo>
<sch:title>Schematron validation</sch:title>
<sch:ns prefix="aiml" uri="http://alicebot.org/2001/AIML-1.0.1"/>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="topic">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="category" type="Category"/>
</xs:sequence>
<xs:attribute name="name" type="SimplePatternExpression" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="category" type="Category"/>
</xs:choice>
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.1"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
传递此方案的代码是
<?xml version="1.0" encoding="ISO-8859-1"?>
<aiml version="1.0.1" xmlns="http://alicebot.org/2001/AIML-1.0.1"
xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://alicebot.org/2001/AIML-1.0.1 ../../resources/schema/AIML.xsd">
<category>
...
而未传递的代码是
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0">
<category>
...
后者看起来更简单,但不是通过。
如果我将枚举更改为
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.1"/>
<xs:enumeration value="1.0"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
它仍然没有通过。
如何快速修复XSD?
为什么Xerces没有给出精确的错误,而只是表现得盲目?
作为参考,以下是您的 XSD,其中包含category
和SimplePatternExpression
存根的类型以完成:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://alicebot.org/2001/AIML-1.0.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sch="http://www.ascc.net/xml/schematron"
targetNamespace="http://alicebot.org/2001/AIML-1.0.1"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
version="1.0"
xml:lang="EN">
<xs:element name="aiml">
<xs:annotation>
<xs:documentation>An AIML object is represented by an aiml element in an XML document.</xs:documentation>
<xs:appinfo>
<sch:title>Schematron validation</sch:title>
<sch:ns prefix="aiml" uri="http://alicebot.org/2001/AIML-1.0.1"/>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="topic">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="category" type="xs:string"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="category" type="xs:string"/>
</xs:choice>
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="1.0.1"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
验证最简单的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0.1">
<category/>
</aiml>
(假设没有与 XSD 的外部链接)将产生错误,例如
[Error] try.xml:2:21: cvc-elt.1.a: Cannot find the declaration of element 'aiml'.
因为解析器从未找到 XSD。
使用通过 xsi:noNamespaceSchemaLocation
正确提示的 XSD 位置验证最简单的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<aiml version="1.0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="try.xsd">
<category/>
</aiml>
将产生错误,例如
[Error] try.xsd:10:26: TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'http://alicebot.org/2001/AIML-1.0.1'.
[Error] try.xml:4:47: cvc-elt.1.a: Cannot find the declaration of element 'aiml'.
现在错误是因为解析器找到了 XSD,并且 XSD 通过 targetNamespace
声明 aiml
元素应位于 http://alicebot.org/2001/AIML-1.0.1
命名空间中。
此时,您必须确定您的意图是符合 XSD(它坚持 XML 实例的元素位于命名空间中),还是您的意图是修改 XSD,以便更简单的 XML 实例有效。
修改 XSD 以消除 XML 上的命名空间要求:
若要允许 XML 在命名空间中有效,请从 XSD 中的 xs:schema
元素中删除 targetNamespace="http://alicebot.org/2001/AIML-1.0.1"
属性。
要符合 XSD,请执行以下操作:
若要将 XML 的元素放入 XSD 所需的命名空间中,请按如下所示修改 XML:
<?xml version="1.0" encoding="UTF-8"?>
<a:aiml version="1.0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a="http://alicebot.org/2001/AIML-1.0.1"
xsi:schemaLocation="http://alicebot.org/2001/AIML-1.0.1 try.xsd">
<a:category/>
</a:aiml>
请注意将 a
声明为 http://alicebot.org/2001/AIML-1.0.1
命名空间的命名空间前缀,以及在 aiml
和 category
元素上使用该前缀。 另请注意通过 xsi:schemaLocation
属性为 http://alicebot.org/2001/AIML-1.0.1
命名空间指定 XSD。
架构包含命名空间 http://alicebot.org/2001/AIML-1.0.1 中元素的定义;当您提供不包含命名空间中元素的实例文档时,架构处理器将找不到匹配的定义。请务必注意,命名空间是元素名称的固有部分。当你明白这一点时,也许你会明白Xerces的信息是绝对正确的。