xml - 在 XSD 中验证名为“元素”的元素?
问题描述
我有这个 XML 文件,我必须使用 XSD 文件对其进行验证。我正在创建 XSD 文件,但我做不到。
我的 XML 文件示例
<datos>
<elemento tipoelemento="CABECERA">
<atributo>
<nombre>VERSION</nombre>
<valor>1.0</valor>
</atributo>
<atributo>
<nombre>BRIGADA</nombre>
<valor>JADSJL</valor>
</atributo>
<atributo>
<nombre>BUZON</nombre>
<valor>ASDKLFJKA</valor>
</atributo>
</elemento>
<elemento tipoelemento="INT">
<atributo>
<nombre>EQUNR</nombre>
<valor>9879979797764644</valor>
</atributo>
<atributo>
<nombre>ZDPYC</nombre>
<valor>N</valor>
</atributo>
<atributo>
<nombre>ZDPATORD</nombre>
<valor />
</atributo>
</elemento>
<elemento tipoelemento="EXT">
<atributo>
<nombre>zaufnr</nombre>
<valor>54737674674</valor>
</atributo>
<atributo>
<nombre>zhoras</nombre>
<valor>6</valor>
</atributo>
<atributo>
<nombre>zpuesto</nombre>
<valor>sdgfsg</valor>
</atributo>
</elemento>
我的 XSD 文件
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:element name="datos">
<xs:complexType>
<xs:sequence>
<xs:element name="elemento" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="elemento" minOccurs="1" maxOccurs="1" type="CABECERA" />
<xs:element name="elemento" minOccurs="0" maxOccurs="unbounded" type="INT" />
<xs:element name="elemento" minOccurs="0" maxOccurs="unbounded" type="EXT" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="CABECERA">
<xs:sequence>
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="VERSION" />
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="BRIGADA" />
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="BUZON" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="VERSION">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="VERSION" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BRIGADA">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="BRIGADA" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BUZON">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="BUZON" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="INT">
<xs:sequence>
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="EQUNR" />
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="ZDPYC" />
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="ZDPATORD" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="EQUNR">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="EQUNR" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ZDPYC">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="ZDPYC" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ZDPATORD">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="ZDPATORD" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="EXT">
<xs:sequence>
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="zaufnr" />
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="zhoras" />
<xs:element name="atributo" minOccurs="1" maxOccurs="1" type="zpuesto" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="zaufnr">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="zaufnr" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="zhoras">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="zhoras" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="zpuesto">
<xs:sequence>
<xs:element name="nombre" type="xs:string" minOccurs="1" fixed="zpuesto" />
<xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:schema>
如何验证此 XSD 中的以下几点?
- 总是,
elementtype="CABECERA"
具有相同的元素attribute.name
并且该元素是必需的 - 总是,
elementtype="INT"
具有相同的元素attribute.name
,但这个元素是可选的。 - 总是,
elementtype="EXT"
具有相同的元素attribute.name
,但这个元素是可选的。
要创建此 xsd,我已点击此链接: 单击此处
但是这个 xsd 抛出了一个异常:
cos-element -istent:错误参数为“CABECERA”。Aparecen en el grupo de modelos varios elementos con el nombre 'attributo' y con tipos diferentes。
我不能放弃这个设计,我应该验证这个 xml。
有人可以帮助我吗?
解决方案
首先,您尝试通过 XSD 表示的 XML 设计是错误的。命名的元素element
是设计缺陷的严重标志(除非您正在为 XSD 编写 XSD)。你真的应该放弃这个设计。
XSD 通常期望能够根据元素的名称来区分元素的类型。而不是
<element elementtype="HEADER">
<attribute>
<name>version</name>
<value>1.0</value>
</attribute>
<attribute>
<name>imei</name>
<value>79873489274902475</value>
</attribute>
</element>
您的 XML 会更好地设计为:
<HEADER version="1.0" imei="79873489274902475"/>
如果您坚持当前的过度元设计,请意识到您将无法根据属性值区分同名元素,除非您能够使用支持条件类型分配 (CTA)的 XSD 1.1 。
但是请注意,即使您可以使用 CTA,但为了支持这种极端的元设计而过度使用它仍然是不明智的。
有关的:
推荐阅读
- c# - MSBuild 为后台任务生成无效的 AppxManifest.xml 文件
- laravel - Laravel:auth.php 中的存储密钥是什么?
- spring - Spring Data REST 重命名资源
- excel - 列出对象表命名范围
- node.js - 如何在 NodeJS 中将 JSON 传递给服务器?
- java - 在 Spring 中使用 @QueryParam
- javascript - 从另一个页面返回时如何强制 HTML 重新加载
- shell - 如何将 perl 转换为 shell 脚本?
- python - Dash plotly 自定义地图
- c++ - 在套接字编程上捕获客户端从服务器退出