从元素中使用XSOM获取minOccurs属性
如何使用XSOM解析器从元素中获取minOccurs属性? 我已经看到这个例子来获取与复杂类型相关的属性:
private void getAttributes(XSComplexType xsComplexType){ Collection c = xsComplexType.getAttributeUses(); Iterator i = c.iterator();while(i.hasNext()){ XSAttributeDecl attributeDecl = i.next().getDecl(); System.out.println("type: "+attributeDecl.getType()); System.out.println("name:"+attributeDecl.getName()); } }
但是,似乎无法找到一种正确的方法来将它从一个元素中取出,例如:
谢谢!
所以这并不是那么直观,但XSElementDecl来自XSParticles。 我能够使用以下代码检索相应的属性:
public boolean isOptional(final String elementName) { for (final Entry 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; }
在xsom
,Element Declaration的类型为XSElementDecl
。 要获得元素的最小和最大出现,您需要获取ParticleImpl
。 即
public int getMinOccurrence(XSElementDecl element){ int min=((ParticleImpl)element.getType()).getMinOccurs(); return min; }
ref: XSOM粒子参考