开发者

getting the minOccurs attribute using XSOM from an element

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<? ex开发者_StackOverflowtends 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!


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;
}


In xsom, Element Declaration is of Type XSElementDecl. For getting the minimum and max occurrence of an element you need to get the ParticleImpl. ie,

public int getMinOccurrence(XSElementDecl element){

 int min=((ParticleImpl)element.getType()).getMinOccurs();
 return min; 

}

ref:XSOM Particle ref

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜