You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Andrew Paterson <an...@burnsecs.com> on 2006/05/23 16:42:44 UTC
Validation of XML Schema (XSD) element groups
I have searched the mailing list archives (user and dev) and checked
Jira but I cannot find anything that exactly matches this potential
problem (most if not all of the issues are with the combination of
"group" with "choice"). This may mean that I am misunderstanding the XSD
spec. but I am sure that someone will let me know if that is the case!
I am trying to create a schema definition for an element that must
contain either or both of two sub-elements. I do not want to make both
sub-elements optional because I want to ensure that users send at least
one of them but I cannot use "choice" because I want users to be able to
send both elements if necessary. I thought that element groups might be
the answer and so this is the schema I created (test_group.xsd):
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="elements" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="elements">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="element1" type="xsd:string" minOccurs="1"
maxOccurs="1"/>
<xsd:group ref="element_group" minOccurs="1"
maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:group name="element_group">
<xsd:sequence>
<xsd:element name="element2" type="xsd:string" minOccurs="0"
maxOccurs="1"/>
<xsd:element name="element3" type="xsd:string" minOccurs="0"
maxOccurs="1"/>
</xsd:sequence>
</xsd:group>
</xsd:schema>
Note that both elements within the group are optional but the group is
specified as mandatory within the definition of "elements" (minOccurs =
1). This is the test data:
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test_group.xsd">
<elements>
<element1>aaaa</element1>
</elements>
<elements>
<element1>aaaa</element1>
<element2>bbbb</element2>
</elements>
<elements>
<element1>aaaa</element1>
<element3>cccc</element3>
</elements>
<elements>
<element1>aaaa</element1>
<element2>bbbb</element2>
<element3>cccc</element3>
</elements>
</root>
I expected the first occurrence of "<elements>" to fail (neither
<element2> nor <element3> is present) but the file passed validation. Is
it Xerces-J (2.8.0), the schema or my understanding that is incorrect
and, if it is not the Xerces-J, is there a way to do what I want in a
W3C schema?
Note: Validation against a DTD showed the same problem.
Thanks,
Andrew
All opinions are completely my own - after all, who else would want to
take credit for them?
---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org
Re: Validation of XML Schema (XSD) element groups
Posted by Klaus Malorny <Kl...@knipp.de>.
Andrew Paterson wrote:
> [...]
>
> I expected the first occurrence of "<elements>" to fail (neither
> <element2> nor <element3> is present) but the file passed validation. Is
> it Xerces-J (2.8.0), the schema or my understanding that is incorrect
> and, if it is not the Xerces-J, is there a way to do what I want in a
> W3C schema?
>
> Note: Validation against a DTD showed the same problem.
>
> Thanks,
> Andrew
>
Hi Andrew,
it is not a bug, it is correct that the schema you've created accepts the
<element> element that contains only the <element1>. As both elements in the
"element_group" group are optional, it matches the non-existing elements after
<element1>.
I'm not a schema expert, but I would create a choice of two, one requiring
element2 and optionally accepting element3, the other requiring element3
(solely). Forgive me any syntax errors, as I am a bit lost without XMLSpy I'm
using at my workplace. But I think you'll understand.
...
<choice>
<sequence>
<element minOccurs="1" name="element2"/>
<element minOccurs="0" name="element3"/>
</sequence>
<sequence>
<element minOccurs="1" name="element3"/>
</sequence>
</choice>
...
Greetings,
Klaus
---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org