|
|
Rank: Newbie
Joined: 7/13/2009 Posts: 2 Location: Pasadena, CA
|
Hi everyone,
I'd like to know how I can modify an existing XSD so it can be more flexible. I have perhaps hundreds of complex elements which are defined with the <sequence> indicator, but the order in which the child elements are given really does not matter. I would like to permit any of the child elements written, but not require them in order (as with sequence indicator), nor require that they are ALL given (as with all indicator). Is there a way I can do this using XSD 2.20?
Bonus question:
I also have at least one complex type defined with <sequence> that contains a "nested" <choice>. If a change can be made to achieve my first goal, will this nested <choice> be affected?
EXAMPLE:
<xsd:complexType name="Sample"> <xsd:sequence> <xsd:element minOccurs="0" ref="name" /> <xsd:choice> <xsd:element ref="Person" /> <xsd:element ref="Organization" /> </xsd:choice> <xsd:element minOccurs="0" maxOccurs="unbounded" ref="Address" /> </xsd:sequence> <xsd:attribute name="id" type="xsd:ID" use="required" /> </xsd:complexType>
|
|
Rank: Newbie
Joined: 10/28/2002 Posts: 1,283 Location: AT
|
Hi newbie,
try using a choice compositor instead of a sequence or all, and then setting the minOccurs to "0" and the maxOccurs to "unbounded" on this compositor. The elements within the choice can then appear or not appear in any order.
|
|
Rank: Newbie
Joined: 7/13/2009 Posts: 2 Location: Pasadena, CA
|
island wrote:Hi newbie,
try using a choice compositor instead of a sequence or all, and then setting the minOccurs to "0" and the maxOccurs to "unbounded" on this compositor. The elements within the choice can then appear or not appear in any order.
Thank you island,
I did try your suggestion, but it looks like <choice> is forcing an either/or situation. Once the first element is accepted, all others generate an error since the "choice" has been made. My understanding is that <choice> does not allow more than one of the child elements to be defined in the XML. Is that true?
Any other suggestions?
|
|
Rank: Advanced Member
Joined: 12/13/2005 Posts: 2,856 Location: Mauritius
|
You should use ALL, but set every element to minOccurs = 0 which would mean they are not required.
|
|
Rank: Newbie
Joined: 10/28/2002 Posts: 1,283 Location: AT
|
RE [quote] I did try your suggestion, but it looks like <choice> is forcing an either/or situation. Once the first element is accepted, all others generate an error since the "choice" has been made. My understanding is that <choice> does not allow more than one of the child elements to be defined in the XML. Is that true[ quote]
if you have an UNBOUNDED choice then you would allow any of the elements to occur and they could occur in any order e.g.:
Code: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="c" type="xs:string"/> <xs:element name="d" type="xs:string"/> <xs:element name="e" type="xs:string"/> </xs:choice> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
All of the following would be valid instances of this file:
Code:
<?xml version="1.0" encoding="UTF-8"?> <!--Sample XML file generated by XMLSpy v2008 rel. 2 sp2 (https://www.altova.com)--> <a xsi:noNamespaceSchemaLocation="test.xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <b> <d/> <e/> <c/> </b> </a>
Code:
<?xml version="1.0" encoding="UTF-8"?> <!--Sample XML file generated by XMLSpy v2008 rel. 2 sp2 (https://www.altova.com)--> <a xsi:noNamespaceSchemaLocation="test.xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <b> <e/> <d/> </b> </a>
etc.. etc..
this would ALSO allow the elements to appear more than once however i.e.:
<?xml version="1.0" encoding="UTF-8"?> <!--Sample XML file generated by XMLSpy v2008 rel. 2 sp2 (https://www.altova.com)--> <a xsi:noNamespaceSchemaLocation="test.xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <b> <e/> <d/> <e/> </b> </a>
whereas an ALL compositor as Vlad points out would allow the elements to appear appear in any order but each element could only appear a maximum of one time.
|
|
|
guest |