Click to See Complete Forum and Search --> : At Least One of Two Possible Elements
Aki Yasuda
01-06-2009, 08:49 AM
I am trying to define an element that requires at least one of two elements. I have used the "all" compositor with the two elements as optional. This ensures that duplicate entries are not allowed; however, the parent element can be null.
<parent> - ALL - <child1> (optional)
|-<child2> (optional)
How do I make parent have at least one child?
Scriptage
01-06-2009, 10:29 AM
What are you using here? DTD? Schema? XSLT?
Aki Yasuda
01-06-2009, 01:50 PM
This is currently just an XSD (schema file)
Scriptage
01-06-2009, 02:28 PM
Try:
<xs:group name="parentNode">
<xs:choice>
<xs:element name="child1" type="xs:string" maxOccurs="1" />
<xs:element name="child2" type="xs:string" maxOccurs="1" />
<xs:sequence>
<xs:element name="child1" type="xs:string" maxOccurs="1" />
<xs:element name="child2" type="xs:string" maxOccurs="1" />
</xs:sequence>
</xs:choice>
</xs:group>
I'm sure there's a better way to do it but this should do the trick.
Aki Yasuda
01-06-2009, 06:28 PM
I placed the following my schema
<xs:group name="parentNode">
<xs:choice>
<xs:element ref="child1"/>
<xs:element ref="child2"/>
<xs:sequence>
<xs:element ref="child1"/>
<xs:element ref="child2"/>
</xs:sequence>
</xs:choice>
</xs:group>
<xs:element name="child1"/>
<xs:element name="child2"/>
And received a error: The content model of model group parentnode is ambiguous
Scriptage
01-07-2009, 08:40 AM
What are you using to validate? The w3c validator shows no errors.
Aki Yasuda
01-07-2009, 01:16 PM
XML Spy
jkmyoung
01-08-2009, 12:08 PM
The choice should be:
<xs:choice>
<xs:sequence>
<xs:element name="child1" type="xs:string"/>
<xs:element name="child2" type="xs:string" minOccurs="1" />
</xs:sequence>
<xs:element name="child2" type="xs:string"/>
</xs:choice>
Aki Yasuda
01-09-2009, 06:01 AM
Thanks
Slight modification to your suggestion works
<xs:element name="ParentNode">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element ref="icms:child1"/>
<xs:element ref="icms:child2" minOccurs="0"/>
</xs:sequence>
<xs:element ref="icms:child2"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="child1"/>
<xs:element name="child2"/>
It requires at least one element under "ParentNode", permits both and does not allow duplicates. Interesting is that if both are present, they have to be in order: child1 and then child2 ... the reverse cause a validation error.