针对 XSD 的 XML 验证:元素 xxx 在命名空间 zzz 中具有无效的子元素 yyy

发布于 2024-12-05 20:06:48 字数 1621 浏览 0 评论 0原文

XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:DataBodyTemperature="Docobo.DataBodyTemperature" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="tDataBodyTemperature">
    <xs:sequence>
      <xs:element name="Answer" type="xs:double" />
      <xs:element minOccurs="0" maxOccurs="1" name="AmbientTemperature" type="xs:double" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="tDataItem">
      <xs:choice>
        <xs:element name="DataBodyTemperature" type="tDataBodyTemperature" />
      </xs:choice>
  </xs:complexType>
  <xs:element name="DataItem">
    <xs:complexType>
      <xs:complexContent mixed="false">
        <xs:extension base="tDataItem">
          <xs:attribute fixed="1" name="SchemaVersion" type="xs:integer" />
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

XML:

<DataItem>
  <DataBodyTemperature xmlns:DataBodyTemperature="Docobo.DataBodyTemperature">
    <DataBodyTemperature:Answer>37.8</DataBodyTemperature:Answer>
    <DataBodyTemperature:AmbientTemperature>28.5</DataBodyTemperature:AmbientTemperature>
  </DataBodyTemperature >
</DataItem>

我收到验证错误: Xml 架构验证失败:元素“DataBodyTemperature”在命名空间“Docobo.DataBodyTemperature”中具有无效的子元素“Answer”。预期可能的元素列表:'Answer'

The XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:DataBodyTemperature="Docobo.DataBodyTemperature" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="tDataBodyTemperature">
    <xs:sequence>
      <xs:element name="Answer" type="xs:double" />
      <xs:element minOccurs="0" maxOccurs="1" name="AmbientTemperature" type="xs:double" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="tDataItem">
      <xs:choice>
        <xs:element name="DataBodyTemperature" type="tDataBodyTemperature" />
      </xs:choice>
  </xs:complexType>
  <xs:element name="DataItem">
    <xs:complexType>
      <xs:complexContent mixed="false">
        <xs:extension base="tDataItem">
          <xs:attribute fixed="1" name="SchemaVersion" type="xs:integer" />
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

The XML:

<DataItem>
  <DataBodyTemperature xmlns:DataBodyTemperature="Docobo.DataBodyTemperature">
    <DataBodyTemperature:Answer>37.8</DataBodyTemperature:Answer>
    <DataBodyTemperature:AmbientTemperature>28.5</DataBodyTemperature:AmbientTemperature>
  </DataBodyTemperature >
</DataItem>

I am getting a validation error:
Xml failed schema validation: The element 'DataBodyTemperature' has invalid child element 'Answer' in namespace 'Docobo.DataBodyTemperature'. List of possible elements expected: 'Answer'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2024-12-12 20:06:48

您的问题是您的架构没有指定目标名称空间 - 因此所有类型都与空名称空间关联。

导致您的错误的原因是架构指定 DataBodyTemperature 元素(位于 null 命名空间中)应仅包含 AnswerAmbientTemperature 元素(两者其中在 null 命名空间中声明),但是在您的文档中,这些元素实际上位于 Docobo.DataBodyTemperature 命名空间中,本质上使它们成为完全不同的元素。

符合给定架构的 xml 示例如下:

<DataItem SchemaVersion="1">
  <DataBodyTemperature>
    <Answer>1</Answer>
    <AmbientTemperature>1</AmbientTemperature>
  </DataBodyTemperature>
</DataItem>

请注意,没有命名空间声明 - 所有元素都在默认命名空间中。但我怀疑您真正想要的是修改您的 xsd,以便它指定目标名称空间。

<xs:schema targetNamespace="Docobo.DataBodyTemperature" xmlns:dbt="Docobo.DataBodyTemperature" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="tDataBodyTemperature">
    <xs:sequence>
      <xs:element name="Answer" type="xs:double" />
      <xs:element minOccurs="0" maxOccurs="1" name="AmbientTemperature" type="xs:double" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="tDataItem">
    <xs:choice>
      <xs:element name="DataBodyTemperature" type="dbt:tDataBodyTemperature" />
    </xs:choice>
  </xs:complexType>
  <xs:element name="DataItem">
    <xs:complexType>
      <xs:complexContent mixed="false">
        <xs:extension base="dbt:tDataItem">
          <xs:attribute fixed="1" name="SchemaVersion" type="xs:integer" />
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

请注意,您现在还需要限定 tDataItemtDataBodyTemperature 类型,因为它们不再在 null 命名空间中声明。

另请注意,在同一个 XML 中,DataItemDataBodyTemperature 元素不在“Docobo.DataBodyTemperature”命名空间中,因此现在不会针对上述架构进行验证。

您可能还会发现获取架构的示例 xml 文档很有帮助 - 您可以在 Visual Studio 2008 或更高版本中使用 XML 架构资源管理器执行此操作,请参阅 如何从 DTD 或 XSD 生成示例 XML 文档?

Your problem is that your schema doesn't specify a target namespace - consequently all types are associated with the null namespace.

Your error is caused because the schema specifies that the DataBodyTemperature element (which is in the null namespace) should contain only Answer and AmbientTemperature elements (both of which are declared in the null namespace), however in your document these elements are in fact in the Docobo.DataBodyTemperature namespace, essentially making them completely different elements.

A sample of xml conforming to your given schema would be:

<DataItem SchemaVersion="1">
  <DataBodyTemperature>
    <Answer>1</Answer>
    <AmbientTemperature>1</AmbientTemperature>
  </DataBodyTemperature>
</DataItem>

Notice that there is no namespace declaration - all elements are in the default namespace. I suspect what you really want however is to modify your xsd so that it specifies a target namespace.

<xs:schema targetNamespace="Docobo.DataBodyTemperature" xmlns:dbt="Docobo.DataBodyTemperature" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="tDataBodyTemperature">
    <xs:sequence>
      <xs:element name="Answer" type="xs:double" />
      <xs:element minOccurs="0" maxOccurs="1" name="AmbientTemperature" type="xs:double" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="tDataItem">
    <xs:choice>
      <xs:element name="DataBodyTemperature" type="dbt:tDataBodyTemperature" />
    </xs:choice>
  </xs:complexType>
  <xs:element name="DataItem">
    <xs:complexType>
      <xs:complexContent mixed="false">
        <xs:extension base="dbt:tDataItem">
          <xs:attribute fixed="1" name="SchemaVersion" type="xs:integer" />
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

Note that you now also need to qualify the tDataItem and tDataBodyTemperature types as they are no longer declared in the null namespace.

Also note that in your sameple XML the DataItem and DataBodyTemperature elements aren't in the "Docobo.DataBodyTemperature" namespace and so now wouldn't be validated against the above schema.

You may also find it helpeful to get a sample xml document for a schema - you can do this in Visual Studio 2008 or later using the XML Schema Explorer, see How to generate sample XML documents from their DTD or XSD?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文