针对 XSD 的 XML 验证:元素 xxx 在命名空间 zzz 中具有无效的子元素 yyy
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您的架构没有指定目标名称空间 - 因此所有类型都与空名称空间关联。
导致您的错误的原因是架构指定
DataBodyTemperature
元素(位于 null 命名空间中)应仅包含Answer
和AmbientTemperature
元素(两者其中在 null 命名空间中声明),但是在您的文档中,这些元素实际上位于 Docobo.DataBodyTemperature 命名空间中,本质上使它们成为完全不同的元素。符合给定架构的 xml 示例如下:
请注意,没有命名空间声明 - 所有元素都在默认命名空间中。但我怀疑您真正想要的是修改您的 xsd,以便它指定目标名称空间。
请注意,您现在还需要限定
tDataItem
和tDataBodyTemperature
类型,因为它们不再在 null 命名空间中声明。另请注意,在同一个 XML 中,
DataItem
和DataBodyTemperature
元素不在“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 onlyAnswer
andAmbientTemperature
elements (both of which are declared in the null namespace), however in your document these elements are in fact in theDocobo.DataBodyTemperature
namespace, essentially making them completely different elements.A sample of xml conforming to your given schema would be:
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.
Note that you now also need to qualify the
tDataItem
andtDataBodyTemperature
types as they are no longer declared in the null namespace.Also note that in your sameple XML the
DataItem
andDataBodyTemperature
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?.