代码之家  ›  专栏  ›  技术社区  ›  Paul Fisher

XSD中的子元素和命名空间

  •  1
  • Paul Fisher  · 技术社区  · 17 年前

    <fun xmlns="http://ttdi.us/I/am/having/fun"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://ttdi.us/I/am/having/fun
                              test.xsd">
        <activity>rowing</activity>
        <activity>eating</activity>
        <activity>coding</activity>
    </fun>
    

    使用以下XSD(诚然,我只是一个凡人):

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://ttdi.us/I/am/having/fun" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ttdi.us/I/am/having/fun">
        <xsd:element name="fun" type="activityList"></xsd:element>
    
        <xsd:complexType name="activityList">
            <xsd:sequence>
                <xsd:element name="activity" type="xsd:string" maxOccurs="unbounded" minOccurs="0"></xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>
    

    cvc-complex-type.2.4.a: Invalid content was found starting with element 'activity'. One of '{activity}' is expected.
    

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://ttdi.us/I/am/having/fun"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ttdi.us/I/am/having/fun">
        <xsd:element name="activity" type="xsd:string"></xsd:element>
    
        <xsd:element name="fun">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element ref="activity" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    

    <actvity> 在我的文档的根?如果 ref 应该按原样替换,那么为什么我不能替换 ref="actvity" name="activity" type="xsd:string"

    DocumentBuilderFactory dbf;
    // initialize dbf
    dbf.setNamespaceAware(true);
    
    1 回复  |  直到 17 年前
        1
  •  1
  •   mechanical_meat nazca    17 年前

    此XSD验证正确 here :

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema targetNamespace="http://ttdi.us/I/am/having/fun" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ttdi.us/I/am/having/fun">
    
      <!-- definition of simple element(s) -->
      <xsd:element name="activity" type="xsd:string"></xsd:element>
    
      <!-- definition of complex element(s) -->
      <xsd:element name="fun">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="activity" maxOccurs="unbounded" minOccurs="0"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    
    </xsd:schema>