如何修复XSD验证错误?

时间:2009-01-15 12:50:09

标签: xml xsd

我有一个带有元素0的XML表单,它格式正确但无效。 当我尝试验证XMLSpy时,我收到以下错误: 空元素'hidden'中不允许任何内容。 以下是我的架构:

<xs:element name="hidden">
    <xs:complexType>
        <xs:attribute name="datatype" type="xs:string" use="optional"/>
        <xs:attribute name="alias" type="xs:string" use="optional"/>
        <xs:attribute name="source" type="xs:string" use="optional"/>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="lookup" type="xs:string" use="optional"/>
    </xs:complexType>
</xs:element>

我需要在上述架构中添加什么来修复此错误? Thanx ml

2 个答案:

答案 0 :(得分:2)

您的“隐藏”元素被定义为空,因为您在架构中没有明确允许子元素的任何内容。我假设你想要像

这样的东西
<hidden *[attributes]*>
   <some_other_element/>
</hidden>

但根据http://www.w3schools.com/Schema/schema_complex_empty.asp,你隐含地将“隐藏”定义为空。您需要定义哪些元素可以出现在“隐藏”中。有很多方法可以做到这一点,我建议首先阅读http://www.w3schools.com/Schema/schema_complex.asp

答案 1 :(得分:1)

正如welbog所说,你定义了一个复杂的空元素。假设您只想要隐藏标记中的文本,您可以沿着这些行编写一个模式:

<xs:element name="hidden">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="datatype" type="xs:string" use="optional"/>
        <xs:attribute name="alias"    type="xs:string" use="optional"/>
        <xs:attribute name="source"   type="xs:string" use="optional"/>
        <xs:attribute name="name"     type="xs:string" use="required"/>
        <xs:attribute name="lookup"   type="xs:string" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

这样,您可以像这样使用一段XML:

<hidden datatype="foo" name="bar">0</hidden>

这里发生的是我将“隐藏”定义为xs:integer扩展(顺便说一句,你可以扩展它所需的任何类型),这意味着“隐藏”元素就像整数元素,但有其他约束,或者在这种情况下有附加属性。

相关问题