Search code examples
javaxsdxerces

Xerces2-j XML Schema Attribute / Element Declaration data type


I'm using Apache's Xerces2-j to parse my XSD. I am trying to get the datatype information for the element / attribute declarations in the XSD.

Here's an example XSD:

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

In this case, I'd want to get the datatypes of the Flavor and ProductID attributes. According to the W3C Schema API and its Xerces2-j implementation, XSAttributeDeclaration's getActualVCType() will get me what I want. But for me that method always returns 45, which is UNAVAILABLE_DT. Is this a bug in Xerces2-j, or am I just understanding the API wrong? If I am, I'd appreciate if someone could point me to the right direction here.


Solution

  • You are looking to use the method

    XSAttributeDeclaration.getTypeDefinition(); // returns XSSimpleTypeDefinition
    

    for simple types and/or possibly

    XSAttributeDeclaration.getEnclosingCTDefinition(); // returns XSComplexTypeDefinition
    

    for complex types.

    The method getActualVCType() is deprecated, and its alternative call getValueConstraintValue().getActualValueType() looks into a so-called value constraint which is not what you are looking for. This argument is also supported by the code in XSAttributeDecl.java:

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

    and

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

    with

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

    suggests that you are indeed getting UNAVAILABLE_DT because it is not set. I suggest looking into the XSSimpleTypeDefinition's methods, it looks promising to me.