如何修复XSD验证错误?

时间:2022-12-03 22:42:51

I have an XML form with an element 0, which is well-formed but not valid. When I try to validate it XMLSpy I get a following error: Nothing is allowed inside empty element 'hidden'. Below is my schema:

我有一个带有元素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>

What do I need to add to the above schema to fix this error? Thanx ml

我需要添加到上述架构以修复此错误? Thanx毫升

2 个解决方案

#1


As welbog noted, you defined a complex empty element. Assuming you want only text within the hidden tag, you could write a schema along theses lines :

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

This way, you can have a piece of XML like this one:

这样,您可以拥有一个像这样的XML:

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

What is going on here is that I defined "hidden" to be an extension of xs:integer (by the way, you can make it extends any type you want), which means that "hidden" elements are like integers element, but with additional constraints, or in this case with additional attributes.

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

#2


Your "hidden" element is defined as being empty since you don't have anything in the schema explicitly allowing child elements. I'm assuming you're wanting something like

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

<hidden *[attributes]*>
   <some_other_element/>
</hidden>

But according to http://www.w3schools.com/Schema/schema_complex_empty.asp you have implicitly defined "hidden" to be empty. You need to define which elements can appear inside "hidden". There are many ways to do this and I suggest starting by reading http://www.w3schools.com/Schema/schema_complex.asp.

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

#1


As welbog noted, you defined a complex empty element. Assuming you want only text within the hidden tag, you could write a schema along theses lines :

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

This way, you can have a piece of XML like this one:

这样,您可以拥有一个像这样的XML:

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

What is going on here is that I defined "hidden" to be an extension of xs:integer (by the way, you can make it extends any type you want), which means that "hidden" elements are like integers element, but with additional constraints, or in this case with additional attributes.

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

#2


Your "hidden" element is defined as being empty since you don't have anything in the schema explicitly allowing child elements. I'm assuming you're wanting something like

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

<hidden *[attributes]*>
   <some_other_element/>
</hidden>

But according to http://www.w3schools.com/Schema/schema_complex_empty.asp you have implicitly defined "hidden" to be empty. You need to define which elements can appear inside "hidden". There are many ways to do this and I suggest starting by reading http://www.w3schools.com/Schema/schema_complex.asp.

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