3

如何使用 XSOM 解析器从元素中获取 minOccurs 属性?我看过这个例子来获取与复杂类型相关的属性:

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

但是,似乎无法找到将其从元素中移除的正确方法,例如:

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

谢谢!

4

2 回答 2

3

所以这并不是那么直观,但 XSElementDecl 来自 XSParticles。我能够使用以下代码检索相应的属性:

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;
}
于 2010-01-19T17:56:32.527 回答
0

xsom中,元素声明是类型XSElementDecl。要获取元素的最小和最大出现次数,您需要获取ParticleImpl. IE,

public int getMinOccurrence(XSElementDecl element){

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

}

参考:XSOM 粒子参考

于 2012-12-07T09:37:16.687 回答