tags:

views:

330

answers:

1

How do I get the minOccurs attribute off of an element using the XSOM parser? I've seen this example for getting the attributes related to a complex type:

private void getAttributes(XSComplexType xsComplexType){
    Collection<? extends XSAttributeUse> c = xsComplexType.getAttributeUses();
    Iterator<? extends XSAttributeUse> i = c.iterator();while(i.hasNext()){
        XSAttributeDecl attributeDecl = i.next().getDecl();
        System.out.println("type: "+attributeDecl.getType());
        System.out.println("name:"+attributeDecl.getName());
    }
}

But, can't seem to figure out the right way for getting it off an an element such as:

<xs:element name="StartDate" type="CommonDateType" minOccurs="0"/>

Thanks!

A: 

So this isn't really that intuitive, but the XSElementDecl come from XSParticles. I was able to retrieve the corresponding attribute with the following code:

public boolean isOptional(final String elementName) {
    for (final Entry<String, XSComplexType> entry : getComplexTypes().entrySet()) {
        final XSContentType content = entry.getValue().getContentType();
        final XSParticle particle = content.asParticle();
        if (null != particle) {
            final XSTerm term = particle.getTerm();
            if (term.isModelGroup()) {
                final XSParticle[] particles = term.asModelGroup().getChildren();
                for (final XSParticle p : particles) {
                    final XSTerm pterm = p.getTerm();
                    if (pterm.isElementDecl()) {
                        final XSElementDecl e = pterm.asElementDecl();
                        if (0 == e.getName().compareToIgnoreCase(elementName)) {
                            return p.getMinOccurs() == 0;
                        }
                    }
                }
             }
          }
    }
    return true;
}
Casey