xml - 这个 XML Schema 有什么根本性的错误吗?

标签 xml xsd schema

我对 XML Schema 仅有初步的了解。这基本上是我第一次以任何严肃的方式与他们互动,我遇到了一些问题。我已经在谷歌上阅读了关于 XSDs 的内容,一切都与这个文件有关。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="credits">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="property" maxOccurs="16" minOccurs="13" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="property" type="xs:string">    
    <xs:complexType>        
        <xs:sequence>            
            <xs:element ref="item" minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute ref="name" use="required"/>
    </xs:complexType>

  </xs:element>

  <xs:element name="item" type="xs:string"/>

  <xs:attribute name="name" type="xs:string">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="example1"/>
          <xs:enumeration value="example2"/>
          <xs:enumeration value="example3"/>
          <xs:enumeration value="example4"/>
          <xs:enumeration value="example5"/>
          <xs:enumeration value="example6"/>
          <xs:enumeration value="example7"/>
          <xs:enumeration value="example8"/>
          <xs:enumeration value="example9"/>
          <xs:enumeration value="example10"/>
          <xs:enumeration value="example11"/>
          <xs:enumeration value="example12"/>
          <xs:enumeration value="example13"/>
          <xs:enumeration value="example14"/>
          <xs:enumeration value="example15"/>
          <xs:enumeration value="example16"/>
        </xs:restriction>
      </xs:simpleType>
  </xs:attribute>

</xs:schema>

这是我加载它的方式:

SchemaFactory schemaFactory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
Schema schemaXSD = schemaFactory.newSchema( new File ( "test.xsd" ) );

我遇到如下异常:

org.xml.sax.SAXParseException: src-element.3: Element 'property' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.

感谢您的帮助!任何关于阅读/使用其他人创建的模式的一般建议也很受欢迎! :D

最佳答案

Element 'property' has both a 'type' attribute and a 'anonymous type' child

换句话说,你说type="xs:string"这规定了像<property>foo</property>这样的节点.但你也放了一个 ComplexType itemproperty里面,这规定了像 <property><item>...</item></property> 这样的节点.这是解析器认为错误的矛盾。

如果要存储多个item每个 property 中的 s 每个property一个单独的字符串,将此字符串存储为单独的节点,或者是带有标签的子节点,或者是 property 的属性.例如。 <property mystring="foo"><item>...</item></property>

关于xml - 这个 XML Schema 有什么根本性的错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5162319/

相关文章:

java - 如何在 xmlID 中使用数字和空格 xmlIDREF JAXB

xml - 如何使用 XSD 1.0 确保序列中只有一个元素有子元素?

java - Jaxb 元素属性选择

meteor - 带有嵌套 autoValue 的 SimpleSchema 无效键

ruby-on-rails - 生成 Rails 应用程序中使用的数据库模式的图像

javascript - 如何创建 string 或 null 类型的 JSON Schema 属性

java - 在Java中从 "Document"替换文件中的XML

java - 将图表存储在数据库中

php - 如何解析 SOAP XML?

xml - 以简单、中立的方式通过 HTTP 发送 XML