【问题标题】:Is there something radically wrong with this XML Schema?这个 XML Schema 有什么根本性的问题吗?
【发布时间】:2011-03-02 01:08:07
【问题描述】:

我对 XML Schema 只有一个初步的了解。这基本上是我第一次以任何严肃的方式与他们互动,我遇到了一些问题。我已经阅读了谷歌上的 XSD,一切看起来都在使用这个文件。

<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:元素“属性”有 一个“类型”属性和一个 '匿名型' 孩子。只有其中之一 这些对于元素是允许的。

感谢您的帮助!任何关于阅读/使用其他人创建的模式的一般建议也值得赞赏! :D

【问题讨论】:

  • 有许多 IDE 可以在编辑时发现这种错误(即在您将 XSD 提交到您自己的代码之前)。例如eclipse J2E 将在右边距显示红色小标记,并带有您引用的相同错误消息))。

标签: xml xsd schema


【解决方案1】:

元素'property'既有'type'属性又有'anonymous type'子元素

换句话说,你说type="xs:string",这规定了像&lt;property&gt;foo&lt;/property&gt;这样的节点。但是你也可以在property 中放置一个ComplexType item,这会规定像&lt;property&gt;&lt;item&gt;...&lt;/item&gt;&lt;/property&gt; 这样的节点。这是解析器认为错误的矛盾。

如果您想在每个property 中存储多个items 并且每个property 一个单独的字符串,将此字符串存储为单独的节点,或者是带有标签的子节点,或property 的属性。例如。 &lt;property mystring="foo"&gt;&lt;item&gt;...&lt;/item&gt;&lt;/property&gt;

【讨论】:

  • 那你怎么知道元素有一个属性而不使用completype呢?我认为一个属性有元素必须在 complexType 中。
【解决方案2】:

这是你的问题代码:

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

要么移除外部元素上的类型 (type="xs:string"),要么移除匿名内部 complexType 元素 (&lt;xs:complexType&gt; ... &lt;/xs:complexType&gt;)

【讨论】:

  • 这可能也会出现在下一段代码中。 。你有两种类型声明,系统不知道使用哪一种。
  • 那你怎么知道元素有一个属性而不使用completype呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多