The element had to have a type of 'a list of strings' and there can be 0
or more occurrence of that type of information. So what I did was:
<xs:element name="xxx" type="ooo" />
<xs:simpleType name="ooo">
<xs:list itemType="xs:string" />
</xs:simpleType>
I believe this is correct, but
where do I put minOccur
and maxOccur
here?
The XML Object language uses the <List> element to represent lists. The contents of the <List> element can be only other XML Objects. In the following example, the content of the <List> element are <String> elements.
The list element defines a simple type element as a list of values of a specified data type.
The data type xs:string represents character strings in XML. Because xs:string is a simple type, it cannot contain any children.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With