[英]How to ensure some mandatory and some optional elements under complextype in xml?
我在制作復雜元素方面遇到了問題,它允許可選元素和強制元素。 對於下面的xml,假設h2是必需的,而h1是可選的,順序無關緊要。
情況1:
<root>
<h1/>
<h2/>
</root>
案例2:
<root>
<h2/>
</root>
案例3:
<root>
<h2/>
<h1/>
</root>
XSD:
<xs:element name="root">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="h1" minOccurs="0"></xs:element>
<xs:element name="h2" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
上面的第三種情況在這個xsd中失敗了,但是這種情況是有效的。 我需要一個對所有上述情況都有效的xsd。
知道你想要的是:
使h2發生在最近1,而h1可以發生盡可能多的次數
如果XML的內容類似於(RegExpr) <root><h1/>*<h2/><h1/>*</root>
可以使用此XSD定義XML有效。
XSD:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="1">
<xsd:element name="h1" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="h2" minOccurs="1" maxOccurs="1"/>
<xsd:element name="h1" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<root> <h1/> <h1/> <h2/> <h1/> </root>
2)
<root> <h1/> <h2/> </root>
<h2/>
元素。
<root> <h2/> <h1/> <h2/> </root>
沒有<h2/>
元素。
<root> <h1/> <h1/> </root>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.