Getting minOccurs attribute using XSOM from element

How do I get the minOccurs attribute of an element using the XSOM parser? I saw this example to get attributes of 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, it seems, it cannot find the right way to get this element, for example:

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

Thank!

+3
source share
2 answers

So this is actually not intuitive, but XSElementDecl comes from XSParticles. I managed to get 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;
}
+3
source

The xsomelement declaration is of type XSElementDecl. To get the minimum and maximum number of elements you need to get ParticleImpl. i.e,

public int getMinOccurrence(XSElementDecl element){

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

}

ref: XSOM Particle ref

0

Source: https://habr.com/ru/post/1729101/


All Articles