[英]How to validate that child elements exist with out specifying the order in XML Schema?
[英]How to validate an xml with repeating child elements
我有一個xml文檔,其中有三個子元素以任何順序重復。 我在xsd中有xsd:sequence元素,因為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
的屬性,因此您可以有任意數量的重復 - >按任意順序選擇包含的任何數量的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.