2012-09-19 62 views
5

元素必須具有「字符串列表」類型,並且可以有0或更多類型的信息。所以我所做的就是:XML Schema - 字符串列表

<xs:element name="xxx" type="ooo" /> 

<xs:simpleType name="ooo"> 
    <xs:list itemType="xs:string" /> 
</xs:simpleType> 

我相信這是正確的,但

我在哪裏把minOccurmaxOccur這裏?

+1

你是什麼意思的「字符串列表」?你想要0到x出現的元素「xxx」?或者你想要一個單一的「xxx」元素,它可以解析一個單獨的分隔字符串?或者你是否想要一個具有0到x個子元素(實際上是你的列表)的單個「xxx」元素 – psubsee2003

+0

psubsee2003如果你不介意,你能否給我所有可能的解釋的例子嗎? –

回答

23

你的問題是不幸不清楚,因爲這可能意味着許多東西。

一種可能的解釋是您希望元素「xxx」出現在0到x次之間。這是通過在根元素內定義一個序列完成的。

<xs:simpleType name="ooo"> 
    <xs:restriction base="xs:string" /> 
</xs:simpleType> 

<xs:element name="root"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="xxx" type="ooo" minOccurs="0" maxOccurs="unbounded" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 

不能在根元素上指定minOccurs和maxOccurs,儘管在XML中只能有1個根元素。但是你可以把一個序列定義爲根元素的一個子元素,這就是上面例子中所做的事情。

現在,如果你想要「xxx」作爲你的根元素,你可以有效地做同樣的事情。這是完全一樣的,除了代替稱爲元素「根」您現在已經被稱爲「XXX」的元素和子元素都被稱爲別的東西有一種類型的「嗚嗚嗚」

<xs:simpleType name="ooo"> 
    <xs:restriction base="xs:string" /> 
</xs:simpleType> 

<xs:element name="xxx"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="xxx-child" type="ooo" maxOccurs="unbounded" minOccurs="0" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 

在您的例子中, 「列表」類型的用法確實意味着我認爲你認爲它的意思。 XSD中的列表類型不定義元素序列,而是可以具有由空格分隔的值列表的單個元素。

<xs:simpleType name="ooo"> 
    <xs:list itemType="xs:string" /> 
</xs:simpleType> 

<xs:element name="xxx" type="ooo" /> 

從這個模式認定中生成的XML應該是這樣的:

<xxx>item1 item2 item3 item4 item5</xxx> 

的XML實際上是用空格分隔的任意長度的字符串列表無限。您可以爲從xs:string繼承的列表定義一個類型來限制值的類型,但我不知道限制長度的方法。

最後,我認爲你試圖完成的工作接近我的上述建議之一,用「xxx」和「xxx-child」,但只是重新格式化序列的定義。

<xs:complexType name="ooo"> 
    <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
    <xs:element name="child" type="xs:string" /> 
    </xs:sequence> 
</xs:complexType> 

<xs:element name="xxx" type="ooo" /> 

而生成的XML將是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<xxx> 
    <child></child> 
    <child></child> 
    <child></child> 
</xxx> 

而且還有最後一個選項其他變化,如移動從<xs:sequence>minOccurs="0" maxOccurs="unbounded"的「孩子」的元素。在你的例子中,根本不重要,因爲兩個定義都會導致相同的XML。但是,如果你有2個元素,這將意味着不同的事情:

<xs:complexType name="ooo"> 
    <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
    <xs:element name="child1" type="xs:string" /> 
    <xs:element name="child2" type="xs:string" /> 
    </xs:sequence> 
</xs:complexType> 

會導致像這樣的XML(child1後續通過的child2序列會重複x次):

<?xml version="1.0" encoding="utf-8"?> 
<xxx> 
    <child1></child1> 
    <child2></child2> 
    <child1></child1> 
    <child2></child2> 
    <child1></child1> 
    <child2></child2> 
</xxx> 

其中作爲

<xs:complexType name="ooo"> 
    <xs:sequence> 
    <xs:element name="child1" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> 
    <xs:element name="child2" type="xs:string" minOccurs="0" maxOccurs="unbounded" /> 
    </xs:sequence> 
</xs:complexType> 

將導致這樣的一個XML(child1將重複x次,隨後的child2重複ÿ時間):

<?xml version="1.0" encoding="utf-8"?> 
<xxx> 
    <child1></child1> 
    <child1></child1> 
    <child2></child2> 
    <child2></child2> 
    <child2></child2> 
    <child2></child2> 
    <child2></child2> 
</xxx> 
+0

這個答案非常精確,完全可以回答我的問題。非常感謝您的詳細解答。 –

-1

下面是一個代碼示例我發現可以幫助你:

<xs:element name="person"> 
<xs:complexType> 
     <xs:sequence> 
      <xs:element name="full_name" type="xs:string"/> 
      <xs:element name="child_name" type="xs:string" 
      maxOccurs="10" minOccurs="0"/> 
     </xs:sequence> 
</xs:complexType> 
</xs:element>