1

I have the following schema which works fine:

<?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>

However, when I try to define an attribute for the top level bindings element, I get an error no matter where I place the attribute code. What am I missing or doing wrong?

EDIT: It looks like there's some problem with either my Java XML code or Xerces. If I change the XSD to give the top-level element the optional "parent" attribute, Xerves gives me the error "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>." However, if I give the attribute any name //other// than "parent", it reports Attribute 'parent' is not allowed to appear in element 'bindings'., just as you'd expect.

My Java code regarding XSD and Xerces is:

    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);

EDIT 2:

The XML file which is being validated:

<bindings parent="game/movement">
    <bind trigger="i" command="INVENTORY"/>
</bindings>
4

1 に答える 1

0

これは機能します (code という名前の属性を参照):

<?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:attribute name="code"/>
        </xs:complexType> 
    </xs:element> 
    <!-- bindings --> 
</xs:schema> 

属性

于 2012-05-10T21:23:45.460 に答える