XML Schema - a list of string

To avoid of getting an object, which has a field, which has a list of strings, you may want to use simpler way:

<xs:complexType>
    <xs:sequence>
        <xs:element name="stringList" block="extension"
            minOccurs="0" maxOccurs="unbounded" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

then in generated class you'll get a field, which directly contains list (not an object)

List<String> stringList;

Your question is unfortunately unclear because it could mean multiple things.

One possible interpretation is that you want an element "xxx" to occur between 0 and x times. That is done by defining a sequence inside of a root element.

<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>

You cannot specify minOccurs and maxOccurs on the root element though as there can be only 1 root element in an XML. But you can define a sequence as a child to the root element, that this what is being done in the above example.

Now if you wanted "xxx" to be your root element, you can effectively do the same exact thing. this is exactly the same except instead of an element called "root" you now have an element called "xxx" and the child elements are called something else with a type of "ooo"

<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>

In your example, the usage of the "list" type does mean what I think you think it means. A list type in an XSD doesn't define a sequence of elements, but a single element that can have a list of values delimited by a space.

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

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

The resulting XML from this schema defination would look like this:

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

The XML is effectively an unbounded list of strings of any length separated by a space. You could define a type for the list that inherits from xs:string to restrict the types of values, but I am not aware of a method to restrict the length.

And lastly, what I think you are trying to accomplish is close to one of my suggestions above with "xxx" and "xxx-child", but just reformatting where the sequence is defined.

<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" />

And the resulting XML would like this:

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

And there are other variations on the last option, such as moving the minOccurs="0" maxOccurs="unbounded" from the <xs:sequence> to the "child" element. In your example it won't matter at all because both definitions would result in the same exact XML. But if you had 2 child elements, it would mean different things:

<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>

Would result in an XML like this (the sequence of child1 follow by child2 would be repeated x times):

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

where as

<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>

would result in an XML like this (child1 would repeat x times, followed by child2 repeating y time):

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

Tags:

Xml

List

Schema