getting the minOccurs attribute using XSOM from an element

你说的曾经没有我的故事 提交于 2019-12-06 01:05:00

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!