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 "Zeitlin, Nicolas" <Ni...@sabre-holdings.com> on 2010/03/30 23:30:49 UTC

Retrieving Minimum and Maximum Occurrences

Hi guys,

 

I have read the past threads regarding minimum and maximum occurrences'
attributes, but I think I've stumbled with a new troublesome scenario.

 

I'm working with a schema that allows multiple instances
(maxOccurs="unbounded") of different elements. The following snippet
should be enough to illustrate:

 

        <xs:element name="bag" maxOccurs="unbounded">

          <xs:complexType>

            <xs:sequence>

              <xs:element name="pocket" maxOccurs="unbounded">

                <xs:complexType>

                    <xs:sequence>

                      <xs:element name="item" maxOccurs="unbounded">

                        <xs:complexType>

                          <xs:attribute name="name" type="xs:string"/>

                        </xs:complexType>

                      </xs:element>

                    </xs:sequence>

                  <xs:attribute name="name" type="xs:string"/>

                </xs:complexType>

              </xs:element>

            </xs:sequence>

            <xs:attribute name="name" type="xs:string"/>

          </xs:complexType>

        </xs:element>

 

 

As you can see from the code above, all three elements, the bag, the
pocket, and the item are allowed to repeat themselves indefinitely.
However, when retrieving the Element Type Information from within the
xerces code, I am not being able to retrieve the item particle
information. Indeed, if I rewrite the pocket so that it has no embedded
sub-element, then it is the pocket element the one with no particle. For
all I can tell from the tests I have done, the last element in the
hierarchy (i.e. the element with no further nested sub-elements) has no
particles, and therefore no available maxOccurs/minOccurs information.

 

I'm retrieving the occurrence information form within my own content
handler, from where I have overridden the startElement() and included
the following snippet:

 

XSParticle p =
((XSComplexTypeDefinition)provider.getElementTypeInfo()).getParticle();

int maxOccurs = ((XSParticleDecl)p).maxEffectiveTotalRange();

 

As I was saying earlier, the particle itself is not available in the
last element of the hierarchy, so the maxOccurs is unavailable. Has
anyone stumbled with this before? Is there another way to retrieve the
attribute, whether it's through the particle or not? 

Thanks in advance,

Nicolas Zeitlin


RE: Retrieving Minimum and Maximum Occurrences

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Nicolas,

"Zeitlin, Nicolas" <Ni...@sabre-holdings.com> wrote on 03/31/2010
09:53:07 AM:

> Hi Michael,
>
> Many thanks for your quick reply. When executing:
>
> XSParticle p = ((XSComplexTypeDefinition)provider.getElementTypeInfo
> ()).getParticle(); // provider is the TypeInfoProvider
>
> ? I am in fact retrieving the XSComplexTypeDefinition, analogously
> to executing getEnclosingCTDefinition().

They are not the same thing. You have the type of the current element. The
value returned from XSElementDeclaration.getEnclosingCTDefinition() is the
type of the parent element and you would need that to determine which
particle the current element was contributed from.

> Indeed, since what I?m
> working with is the complex type object and not the element object,
> calling the getEnclosingCTDefinition() method is impossible. My
> problem is that the XSComplexTypeDefinition, I believed, should have
> the particle information. This is the case for the other complex
> elements in my schema, in a higher lever of hierarchy.
>
> Do you think this Is this the intended behavior? Forgive me if I
> repeat myself, but I may not have understood your answer then?

You could get to the XSElementDeclaration if you were using the
PSVIProvider [1]. See this FAQ [2] on how to obtain one.

> Thank you very much for helping me out with this.
> Nicolas
>
>
> From: Michael Glavassevich [mailto:mrglavas@ca.ibm.com]
> Sent: Tuesday, March 30, 2010 8:10 PM
> To: j-users@xerces.apache.org
> Subject: Re: Retrieving Minimum and Maximum Occurrences
>
> Hi Nicolas,
>
> Particles are not part of the PSVI in XML Schema 1.0. You would have
> to walk back up to the complex type definition (see
> getEnclosingCTDefinition() on XSElementDeclaration) then search for
> the particle which points to the element declaration within that
> complex type definition. Note that this doesn't always work (could
> have multiple particles pointing to the same global element
> declaration) but is about the best you can do unless you're willing
> to build a state machine to keep track of where you are in the schema
grammar.
>
> Thanks.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org

[1]
http://xerces.apache.org/xerces2-j/javadocs/xs/org/apache/xerces/xs/PSVIProvider.html
[2] http://xerces.apache.org/xerces2-j/faq-xs.html#faq-8

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

RE: Retrieving Minimum and Maximum Occurrences

Posted by "Zeitlin, Nicolas" <Ni...@sabre-holdings.com>.
Hi Michael,

 

Many thanks for your quick reply. When executing:

 

XSParticle p =
((XSComplexTypeDefinition)provider.getElementTypeInfo()).getParticle();
// provider is the TypeInfoProvider

 

... I am in fact retrieving the XSComplexTypeDefinition, analogously to
executing getEnclosingCTDefinition(). Indeed, since what I'm working
with is the complex type object and not the element object, calling the
getEnclosingCTDefinition() method is impossible. My problem is that the
XSComplexTypeDefinition, I believed, should have the particle
information. This is the case for the other complex elements in my
schema, in a higher lever of hierarchy.

 

Do you think this Is this the intended behavior? Forgive me if I repeat
myself, but I may not have understood your answer then...

 

Thank you very much for helping me out with this.

Nicolas

 

________________________________

From: Michael Glavassevich [mailto:mrglavas@ca.ibm.com] 
Sent: Tuesday, March 30, 2010 8:10 PM
To: j-users@xerces.apache.org
Subject: Re: Retrieving Minimum and Maximum Occurrences

 

Hi Nicolas,

Particles are not part of the PSVI in XML Schema 1.0. You would have to
walk back up to the complex type definition (see
getEnclosingCTDefinition() on XSElementDeclaration) then search for the
particle which points to the element declaration within that complex
type definition. Note that this doesn't always work (could have multiple
particles pointing to the same global element declaration) but is about
the best you can do unless you're willing to build a state machine to
keep track of where you are in the schema grammar.

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

"Zeitlin, Nicolas" <Ni...@sabre-holdings.com> wrote on
03/30/2010 05:30:49 PM:

> Hi guys,
>  
> I have read the past threads regarding minimum and maximum 
> occurrences' attributes, but I think I've stumbled with a new 
> troublesome scenario.
>  
> I'm working with a schema that allows multiple instances 
> (maxOccurs="unbounded") of different elements. The following snippet
> should be enough to illustrate:
>  
>         <xs:element name="bag" maxOccurs="unbounded">
>           <xs:complexType>
>             <xs:sequence>
>               <xs:element name="pocket" maxOccurs="unbounded">
>                 <xs:complexType>
>                     <xs:sequence>
>                       <xs:element name="item" maxOccurs="unbounded">
>                         <xs:complexType>
>                           <xs:attribute name="name" type="xs:string"/>
>                         </xs:complexType>
>                       </xs:element>
>                     </xs:sequence>
>                   <xs:attribute name="name" type="xs:string"/>
>                 </xs:complexType>
>               </xs:element>
>             </xs:sequence>
>             <xs:attribute name="name" type="xs:string"/>
>           </xs:complexType>
>         </xs:element>
>  
>  
> As you can see from the code above, all three elements, the bag, the
> pocket, and the item are allowed to repeat themselves indefinitely. 
> However, when retrieving the Element Type Information from within 
> the xerces code, I am not being able to retrieve the item particle 
> information. Indeed, if I rewrite the pocket so that it has no 
> embedded sub-element, then it is the pocket element the one with no 
> particle. For all I can tell from the tests I have done, the last 
> element in the hierarchy (i.e. the element with no further nested 
> sub-elements) has no particles, and therefore no available 
> maxOccurs/minOccurs information.
>  
> I'm retrieving the occurrence information form within my own content
> handler, from where I have overridden the startElement() and 
> included the following snippet:
>  
> XSParticle p = ((XSComplexTypeDefinition)provider.getElementTypeInfo
> ()).getParticle();
> int maxOccurs = ((XSParticleDecl)p).maxEffectiveTotalRange();
>  
> As I was saying earlier, the particle itself is not available in the
> last element of the hierarchy, so the maxOccurs is unavailable. Has 
> anyone stumbled with this before? Is there another way to retrieve 
> the attribute, whether it's through the particle or not? 
> Thanks in advance,
> Nicolas Zeitlin


Re: Retrieving Minimum and Maximum Occurrences

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Nicolas,

Particles are not part of the PSVI in XML Schema 1.0. You would have to
walk back up to the complex type definition (see getEnclosingCTDefinition()
on XSElementDeclaration) then search for the particle which points to the
element declaration within that complex type definition. Note that this
doesn't always work (could have multiple particles pointing to the same
global element declaration) but is about the best you can do unless you're
willing to build a state machine to keep track of where you are in the
schema grammar.

Thanks.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

"Zeitlin, Nicolas" <Ni...@sabre-holdings.com> wrote on 03/30/2010
05:30:49 PM:

> Hi guys,
>
> I have read the past threads regarding minimum and maximum
> occurrences? attributes, but I think I?ve stumbled with a new
> troublesome scenario.
>
> I?m working with a schema that allows multiple instances
> (maxOccurs=?unbounded?) of different elements. The following snippet
> should be enough to illustrate:
>
>         <xs:element name="bag" maxOccurs="unbounded">
>           <xs:complexType>
>             <xs:sequence>>
>               <xs:element name="pocket" maxOccurs="unbounded">
>                 <xs:complexType>
>                     <xs:sequence>
>                       <xs:element name="item" maxOccurs="unbounded">
>                         <xs:complexType>
>                           <xs:attribute name="name" type="xs:string"/>
>                         </xs:complexType>
>                       </xs:element>
>                     </xs:sequence>>
>                   <xs:attribute name="name" type="xs:string"/>
>                 </xs:complexType>
>               </xs:element>
>             </xs:sequence>>
>             <xs:attribute name="name" type="xs:string"/>
>           </xs:complexType>
>         </xs:element>
>
>
> As you can see from the code above, all three elements, the bag, the
> pocket, and the item are allowed to repeat themselves indefinitely.
> However, when retrieving the Element Type Information from within
> the xerces code, I am not being able to retrieve the item particle
> information. Indeed, if I rewrite the pocket so that it has no
> embedded sub-element, then it is the pocket element the one with no
> particle. For all I can tell from the tests I have done, the last
> element in the hierarchy (i.e. the element with no further nested
> sub-elements) has no particles, and therefore no available
> maxOccurs/minOccurs information.
>
> I?m retrieving the occurrence information form within my own content
> handler, from where I have overridden the startElement() and
> included the following snippet:
>
> XSParticle p = ((XSComplexTypeDefinition)provider.getElementTypeInfo
> ()).getParticle();
> int maxOccurs = ((XSParticleDecl)p).maxEffectiveTotalRange();
>
> As I was saying earlier, the particle itself is not available in the
> last element of the hierarchy, so the maxOccurs is unavailable. Has
> anyone stumbled with this before? Is there another way to retrieve
> the attribute, whether it?s through the particle or not?
> Thanks in advance,
> Nicolas Zeitlin