【问题标题】:Xerces2-j XML Schema Attribute / Element Declaration data typeXerces2-j XML 模式属性/元素声明数据类型
【发布时间】:2010-11-15 15:42:47
【问题描述】:

我正在使用 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 APIits Xerces2-j implementation,XSAttributeDeclaration 的 getActualVCType() 将得到我想要的。但对我来说,该方法总是返回 45,即 UNAVAILABLE_DT。这是 Xerces2-j 中的错误,还是我只是理解 API 错误?如果我是,如果有人能在这里指出正确的方向,我将不胜感激。

【问题讨论】:

    标签: java xsd xerces


    【解决方案1】:

    你正在寻找使用方法

    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 的方法,它看起来很有希望。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多