【问题标题】:How to make tag visible to Xerces? Error: "Cannot find the declaration of element"如何使标签对 Xerces 可见?错误:“找不到元素的声明”
【发布时间】:2013-10-30 17:29:49
【问题描述】:

当使用 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 没有给出精确的错误,而只是表现得盲目?

【问题讨论】:

    标签: xml xsd xerces xsd-validation aiml


    【解决方案1】:

    作为参考,这里是您的 XSD,其中 categorySimplePatternExpression 的类型被删除以完成:

    <?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 命名空间的命名空间前缀,以及在 aimlcategory 元素上使用该前缀。还要注意通过xsi:schemaLocation 属性为http://alicebot.org/2001/AIML-1.0.1 命名空间指定XSD。

    【讨论】:

      【解决方案2】:

      您的架构包含命名空间http://alicebot.org/2001/AIML-1.0.1 中元素的定义;当您提供一个包含非命名空间元素的实例文档时,模式处理器将找不到匹配的定义。重要的是要知道命名空间是元素名称的固有部分。当您了解这一点时,也许您会意识到 Xerces 的信息是绝对正确的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        • 1970-01-01
        • 2019-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多