2013-01-22 37 views
1

我想在我的XSD文件中定義兩個元素,這個元素可以在很多時間出現「無界限」,但他們必須總是一個接一個地來。例如:如何使用XSD定義XML中的交替元素

XML的文件:

<company/> 
<address/> 
<customer/> 
<customerContact/> 
<customer/> 
<customerContact/> 
<customer/> 
<customerContact/> 

現在的問題是,下面的XSD定義

<xs:element name="company" type="companyType"/> 
<xs:element name="address" type="addressType"/> 
<xs:element name="customer" type="customerType" maxOccurs="unbounded"/> 
<xs:element name="customerContact" type="customerContactType" maxOccurs="unbounded"/> 

只處理XML文件一樣

<company/> 
<address/> 
<customer/> 
<customer/> 
<customer/> 
<customerContact/> 
<customerContact/> 
<customerContact/> 

其中客戶customerConta ct不交替。我有定義一個元素的想法,它說客戶customerContact並允許重複該元素。這將解決我的問題,但它也會允許其他解決方案不應被接受爲有效。

我認爲清潔的解決辦法是有像XML

<company/> 
<address/> 
<customerEnvelope> 
    <customer/> 
    <customerContact/> 
<customerEnvelope> 
<customerEnvelope> 
    <customer/> 
    <customerContact/> 
</customerEnvelope> 

,並重復customerEnvelope。不幸的是,我的客戶已經提供了XML結構,所以我無法在這裏進行更改。

是否可以使用XSD定義這種結構,還是需要使用上述解決方法?

回答

3
<xs:element name="company" type="companyType"/> 
<xs:element name="address" type="addressType"/> 
<xs:sequence maxOccurs="unbounded"> 
    <xs:element name="customer" type="customerType"/> 
    <xs:element name="customerContact" type="customerContactType"/> 
</xs:sequence> 
+0

這是一個非常簡單的解決方案 - 有時最明顯的事情是最困難的。非常感謝@ 13ren! –

+0

@MathiasBader謝謝,XSD在某些方面相當不錯。順便說一句:你可以把它想像爲正則表達式ab(cd)*' – 13ren

相關問題