验证 XSD 中名为 "element" 的元素?



我有这个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 中的以下几点?

  1. 始终,具有elementtype="CABECERA"的元素具有相同的attribute.name并且此元素是必需的
  2. 始终,带有elementtype="INT"的元素具有相同的attribute.name,但此元素是可选的。
  3. 始终,带有elementtype="EXT"的元素具有相同的attribute.name,但此元素是可选的。

要创建此xsd,我点击了以下链接: 点击这里

但是这个XSD会抛出一个异常:

cos-element -istent: Error para el tipo 'CABECERA'.Aparecen en el grupo de modelos varios elementos con el nombre 'atributo' 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,过度使用CTA仍然不明智,以支持这种极端的元设计。

相关:

  • 如何使用条件类型赋值使类型依赖于属性值
  • 我应该使用 XSD 1.1 来构建开放标准吗?

最新更新