gpt4 book ai didi

xml - 如何验证具有重复子元素的 xml

转载 作者:数据小太阳 更新时间:2023-10-29 02:50:42 25 4
gpt4 key购买 nike

我有一个 xml 文档,其中三个子元素以任意顺序重复。我有 xsd:xsd 中的序列元素,因此 xml 未得到验证。我不能使用 xsd:all,因为元素出现不止一次。

请帮我解决这个问题。

这是xml

 <Trailer>      
<TrailerField name="SegmentLabelOne" length="4" type="String">TSTS</TrailerField>
<TrailerField name="SegmentLabelTwo" length="2" type="String">00</TrailerField>
<CountItem length="10" type="Numeric">MT</CountItem>
<TrailerField name="SegmentLabelThree" length="2" type="String">01</TrailerField>
<CountItem length="10" type="Numeric">MA</CountItem>
<TrailerField name="SegmentLabelFour" length="2" type="String">02</TrailerField>
<TrailerField name="FilerOne" length="65" type="String"> </TrailerField>
</Trailer>

这是xsd

<xsd:complexType name="TrailerSegment">
<xsd:sequence>
<xsd:element name="NameOfElement" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="name" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="CountItem" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="length" type="xsd:string"></xsd:attribute>
<xsd:attribute name="type" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="TrailerField" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="name" type="xsd:string"></xsd:attribute>
<xsd:attribute name="length" type="xsd:string"></xsd:attribute>
<xsd:attribute name="type" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

最佳答案

你需要这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Trailer">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="TrailerField">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="length" type="xs:unsignedByte" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="CountItem">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="length" type="xs:unsignedByte" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

<xs:choice>给你选择里面的任何一个元素的选择,并且自 <xs:choice>具有 maxOccurs=unbounded 的属性,因此您可以有任意数量的重复 --> 您可以按任意顺序选择任意数量的这些元素。

关于xml - 如何验证具有重复子元素的 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com