Discussion:
How to get minOccurs and maxOccurs values from Element Declarations
Stella Lok
2007-11-29 12:25:14 UTC
Permalink
Hi,

I would like to ask how one can retrieve the minOccurs and maxOccurs values
from an element declaration (whether it is is a simple type or is referring
to a simpleType/complexType).
From what I've found in the Xerces API, getMinOccurs() and getMaxOccurs()
are only defined in XSParticle.

Would greatly appreciate if someone could point me in the right direction!

Thanks,
Stella
Michael Glavassevich
2007-11-29 14:16:46 UTC
Permalink
Hi Stella,

You're asking for something you can't get.

Consider that one element declaration could be used in multiple places:

<xs:element name="foo" type="xs:string"/>
<xs:complexType name="bar">
<xs:sequence>
...
<xs:element ref="foo" minOccurs="2" maxOccurs="4"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="baz">
<xs:sequence>
...
<xs:element ref="foo" minOccurs="3" maxOccurs="5"/>
</xs:sequence>
</xs:complexType>

It's the particle (i.e. the usage of the element declaration) [1] which
holds the occurrence information not the element declaration itself.

Thanks.

[1] http://www.w3.org/TR/xmlschema-1/#cParticles

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
Post by Stella Lok
Hi,
I would like to ask how one can retrieve the minOccurs and maxOccurs
values from an element declaration (whether it is is a simple type
or is referring to a simpleType/complexType).
From what I've found in the Xerces API, getMinOccurs() and
getMaxOccurs() are only defined in XSParticle.
Would greatly appreciate if someone could point me in the right direction!
Thanks,
Stella
Wax, Ed
2007-11-29 17:15:36 UTC
Permalink
Stella,

It is not as straight forward as one might image. Assuming you have
navigated to a complex type:



XSComplexTypeDefinition complex; // is set to the current complex
type.



XSParticle p = complex.getParticle();

XSTerm term = p.getTerm();

XSModelGroup xm = (XSModelGroup)term;

XSObjectList xobj = xm.getParticles();



for (int i = 0; i < xobj.getLength(); ++i) {

XSParticle xp = (XSParticle)xobj.item(i);

XSTerm t = xp.getTerm();

if (t instanceof XSElementDeclaration) {

XSElementDeclaration elem = (XSElementDeclaration)t;



// At this point we know the particle is an element.

// Here we can query for optionality info from the partical.

if ( xp != null ) {

isOptional = xp.getMinOccurs() == 0;

// Check here for MaxOccurs as well.

}

}

}



Hope this helps.



Ed



From: Stella Lok [mailto:***@gmail.com]
Sent: Thursday, November 29, 2007 5:25 AM
To: j-***@xerces.apache.org
Subject: How to get minOccurs and maxOccurs values from Element
Declarations



Hi,

I would like to ask how one can retrieve the minOccurs and maxOccurs
values from an element declaration (whether it is is a simple type or is
referring to a simpleType/complexType).
From what I've found in the Xerces API, getMinOccurs() and
getMaxOccurs() are only defined in XSParticle.

Would greatly appreciate if someone could point me in the right
direction!

Thanks,
Stella
Stella Lok
2007-11-29 17:42:13 UTC
Permalink
Hi Ed and Michael,

Thank you very much for taking the time to explain the structure to me! I am
still unclear on 2 specific cases though, and that is when an element
declaration is a simple type.

Case 1:

<xs:element name="simpleElement" minOccurs="0" and maxOccurs="2">
<xs:simpleType>
....
</xs:simpleType>
</xs:element>

I can't figure out how to obtain the minOccurs and maxOccurs values for
simpleElement, since there is no particle for a simpleType?


Case 2:

<xs:element name="simpleElement">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:element name="element2" ref="simpleElement" minOccurs="0" and
maxOccurs="2">
</xs:element>

I suspect that the use of ref means that the full definition of element2
becomes (pardon my lack of proper terms):
<xs:element name="element2" minOccurs="0" and maxOccurs="2">
<xs:complexType>
<xs:element type="simpleElement"/>
</xs:complexType>
</xs:element>

i.e, there is an anonymous complexType and it contains the particle that
defines the occurrences.


I would really like to find out about Case 1, and also seek confirmation on
Case 2.
Thanks very much!

Sincerely,
Stella
Post by Michael Glavassevich
Stella,
It is not as straight forward as one might image. Assuming you have
XSComplexTypeDefinition complex; // is set to the current complex type.
XSParticle p = complex.getParticle();
XSTerm term = p.getTerm();
XSModelGroup xm = (XSModelGroup)term;
XSObjectList xobj = xm.getParticles();
for (int i = 0; i < xobj.getLength(); ++i) {
XSParticle xp = (XSParticle)xobj.item(i);
XSTerm t = xp.getTerm();
if (t instanceof XSElementDeclaration) {
XSElementDeclaration elem = (XSElementDeclaration)t;
// At this point we know the particle is an element.
// Here we can query for optionality info from the partical.
if ( xp != null ) {
isOptional = xp.getMinOccurs() == 0;
// Check here for MaxOccurs as well.
}
}
}
Hope this helps.
Ed
*Sent:* Thursday, November 29, 2007 5:25 AM
*Subject:* How to get minOccurs and maxOccurs values from Element
Declarations
Hi,
I would like to ask how one can retrieve the minOccurs and maxOccurs
values from an element declaration (whether it is is a simple type or is
referring to a simpleType/complexType).
From what I've found in the Xerces API, getMinOccurs() and getMaxOccurs()
are only defined in XSParticle.
Would greatly appreciate if someone could point me in the right direction!
Thanks,
Stella
Michael Glavassevich
2007-11-29 18:37:10 UTC
Permalink
Hi Stella,
Post by Stella Lok
Hi Ed and Michael,
Thank you very much for taking the time to explain the structure to
me! I am still unclear on 2 specific cases though, and that is when
an element declaration is a simple type.
<xs:element name="simpleElement" minOccurs="0" and maxOccurs="2">
<xs:simpleType>
....
</xs:simpleType>
</xs:element>
I can't figure out how to obtain the minOccurs and maxOccurs values
for simpleElement, since there is no particle for a simpleType?
That element declaration is only legal if its enclosed in a complex type,
in other words a local element declaration. You need to walk the enclosing
complex type (XSElementDeclaration.getEnclosingCTDefinition() will get you
that) and find the particle containing this element declaration. The code
which Ed posted shows how to do that.
Post by Stella Lok
<xs:element name="simpleElement">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="element2" ref="simpleElement" minOccurs="0" and
maxOccurs="2">
</xs:element>
I suspect that the use of ref means that the full definition of
<xs:element name="element2" minOccurs="0" and maxOccurs="2">
<xs:complexType>
<xs:element type="simpleElement"/>
</xs:complexType>
</xs:element>
It doesn't mean anything because it's not legal anywhere in a schema
document. You can never specify both "name" and "ref" on xs:element.
Post by Stella Lok
i.e, there is an anonymous complexType and it contains the particle
that defines the occurrences.
I would really like to find out about Case 1, and also seek
confirmation on Case 2.
Thanks very much!
Sincerely,
Stella
Stella,
It is not as straight forward as one might image. Assuming you have
XSComplexTypeDefinition complex; // is set to the current complex type.
XSParticle p = complex.getParticle();
XSTerm term = p.getTerm();
XSModelGroup xm = (XSModelGroup)term;
XSObjectList xobj = xm.getParticles();
for (int i = 0; i < xobj.getLength(); ++i) {
XSParticle xp = (XSParticle)xobj.item(i);
XSTerm t = xp.getTerm();
if (t instanceof XSElementDeclaration) {
XSElementDeclaration elem = (XSElementDeclaration)t;
// At this point we know the particle is an element.
// Here we can query for optionality info from the partical.
if ( xp != null ) {
isOptional = xp.getMinOccurs() == 0;
// Check here for MaxOccurs as well.
}
}
}
Hope this helps.
Ed
Sent: Thursday, November 29, 2007 5:25 AM
Subject: How to get minOccurs and maxOccurs values from Element
Declarations
Post by Stella Lok
Hi,
I would like to ask how one can retrieve the minOccurs and maxOccurs
values from an element declaration (whether it is is a simple type
or is referring to a simpleType/complexType).
From what I've found in the Xerces API, getMinOccurs() and
getMaxOccurs() are only defined in XSParticle.
Would greatly appreciate if someone could point me in the right direction!
Thanks,
Stella
Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: ***@ca.ibm.com
E-mail: ***@apache.org
Stella Lok
2007-11-30 01:35:34 UTC
Permalink
I understand now, thanks a lot for your kind help! I had been stumped by
this.

Sincerely,
Stella
Post by Michael Glavassevich
Hi Stella,
Post by Stella Lok
Hi Ed and Michael,
Thank you very much for taking the time to explain the structure to
me! I am still unclear on 2 specific cases though, and that is when
an element declaration is a simple type.
<xs:element name="simpleElement" minOccurs="0" and maxOccurs="2">
<xs:simpleType>
....
</xs:simpleType>
</xs:element>
I can't figure out how to obtain the minOccurs and maxOccurs values
for simpleElement, since there is no particle for a simpleType?
That element declaration is only legal if its enclosed in a complex type,
in other words a local element declaration. You need to walk the enclosing
complex type (XSElementDeclaration.getEnclosingCTDefinition() will get you
that) and find the particle containing this element declaration. The code
which Ed posted shows how to do that.
Post by Stella Lok
<xs:element name="simpleElement">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="element2" ref="simpleElement" minOccurs="0" and
maxOccurs="2">
</xs:element>
I suspect that the use of ref means that the full definition of
<xs:element name="element2" minOccurs="0" and maxOccurs="2">
<xs:complexType>
<xs:element type="simpleElement"/>
</xs:complexType>
</xs:element>
It doesn't mean anything because it's not legal anywhere in a schema
document. You can never specify both "name" and "ref" on xs:element.
Post by Stella Lok
i.e, there is an anonymous complexType and it contains the particle
that defines the occurrences.
I would really like to find out about Case 1, and also seek
confirmation on Case 2.
Thanks very much!
Sincerely,
Stella
Stella,
It is not as straight forward as one might image. Assuming you have
XSComplexTypeDefinition complex; // is set to the current complex
type.
Post by Stella Lok
XSParticle p = complex.getParticle();
XSTerm term = p.getTerm();
XSModelGroup xm = (XSModelGroup)term;
XSObjectList xobj = xm.getParticles();
for (int i = 0; i < xobj.getLength(); ++i) {
XSParticle xp = (XSParticle)xobj.item(i);
XSTerm t = xp.getTerm();
if (t instanceof XSElementDeclaration) {
XSElementDeclaration elem = (XSElementDeclaration)t;
// At this point we know the particle is an element.
// Here we can query for optionality info from the partical.
if ( xp != null ) {
isOptional = xp.getMinOccurs() == 0;
// Check here for MaxOccurs as well.
}
}
}
Hope this helps.
Ed
Sent: Thursday, November 29, 2007 5:25 AM
Subject: How to get minOccurs and maxOccurs values from Element
Declarations
Post by Stella Lok
Hi,
I would like to ask how one can retrieve the minOccurs and maxOccurs
values from an element declaration (whether it is is a simple type
or is referring to a simpleType/complexType).
From what I've found in the Xerces API, getMinOccurs() and
getMaxOccurs() are only defined in XSParticle.
Would greatly appreciate if someone could point me in the right
direction!
Post by Stella Lok
Thanks,
Stella
Michael Glavassevich
XML Parser Development
IBM Toronto Lab
---------------------------------------------------------------------
Loading...