我有以下工作正常的架构:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bindings">
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="bind">
                    <xs:complexType>
                        <xs:attribute name="trigger" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute> <!-- trigger -->
                        <xs:attribute name="command" use="required">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:attribute> <!-- command -->
                    </xs:complexType>
                </xs:element> <!-- bind -->
            </xs:sequence>
        </xs:complexType>
    </xs:element> <!-- bindings -->
 </xs:schema>
但是,当我尝试为顶级bindings元素定义属性时,无论我将属性代码放在何处,都会出现错误。我错过了什么或做错了什么?
编辑:看起来我的 Java XML 代码或 Xerces 有一些问题。如果我更改 XSD 为顶级元素提供可选的“父”属性,Xerves 会给我错误"Problem: schema_reference.4: Failed to read schema document 'null', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>."  但是,如果我给属性任何名称 //other// 而不是“父”,它会报告Attribute 'parent' is not allowed to appear in element 'bindings'.,就像你一样d 期望。
我关于 XSD 和 Xerces 的 Java 代码是:
    bindingsDocumentBuilderFactory =
        DocumentBuilderFactory.newInstance();
    DocumentBuilderFactory bdbf = bindingsDocumentBuilderFactory;
    bdbf.setValidating(true);
    // I get the input stream here as "is"
    bdbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                      "http://www.w3.org/2001/XMLSchema");
    bdbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", is);
编辑 2:
正在验证的 XML 文件:
<bindings parent="game/movement">
    <bind trigger="i" command="INVENTORY"/>
</bindings>
