Xerces2-j XML Schema 属性/元素声明数据类型

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的理解有误?如果是后者,我希望有人能指点我正确方向。
1个回答

0
你正在寻找使用该方法。
XSAttributeDeclaration.getTypeDefinition(); // returns XSSimpleTypeDefinition

对于简单类型和/或可能的情况

XSAttributeDeclaration.getEnclosingCTDefinition(); // returns XSComplexTypeDefinition

针对复杂类型。

方法getActualVCType()已被弃用,其替代调用getValueConstraintValue().getActualValueType()查看所谓的value constraint, 这不是您要寻找的内容。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的方法,它对我很有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接