3

我正在使用 Apache 的 Xerces2-j 来解析我的 XSD。我正在尝试获取 XSD 中元素/属性声明的数据类型信息。

这是一个示例 XSD:

<xs:element name="Pretzel">
    ...
    <xs:attribute name="Flavor" type="xs:string"/>
    <xs:attribute name="ProductID" type="xs:nonNegativeInteger"/>
    ...
</xs:element>

在这种情况下,我想获取FlavorProductID属性的数据类型。根据W3C Schema API及其Xerces2-j 实现,XSAttributeDeclaration 的 getActualVCType() 将得到我想要的。但对我来说,该方法总是返回 45,即UNAVAILABLE_DT。这是 Xerces2-j 中的错误,还是我只是理解 API 错误?如果我是,如果有人能在这里指出正确的方向,我将不胜感激。

4

1 回答 1

0

您正在寻找使用该方法

XSAttributeDeclaration.getTypeDefinition(); // returns XSSimpleTypeDefinition

对于简单类型和/或可能

XSAttributeDeclaration.getEnclosingCTDefinition(); // returns XSComplexTypeDefinition

对于复杂类型。

方法 getActualVCType() 已弃用,它的替代调用 getValueConstraintValue().getActualValueType() 查找所谓的值约束 ,这不是您要寻找的。XSAttributeDecl.java中的代码也支持此参数:

       // variable definition
48     // value constraint type: default, fixed or !specified
49     short fConstraintType = XSConstants.VC_NONE;

183    public short getActualVCType() {
184        return getConstraintType() == XSConstants.VC_NONE ?
185               XSConstants.UNAVAILABLE_DT :
186               fDefault.actualValueType;
187    }

136
137    public short getConstraintType() {
138        return fConstraintType;
139    }

表明您确实得到了 UNAVAILABLE_DT,因为它没有设置。我建议研究 XSSimpleTypeDefinition 的方法,它看起来很有希望。

于 2011-02-17T03:57:51.870 回答