Question

i am trying to generate schema using jaxb from my exisitng POJO classes and till now its working fine now i have a requirement where i need to declare a attribute type is my XSD but the attribute value should be one of the predefined values. below is the code snap shot from my class

private String destinationID;
private String contactNo;
private String type;
@XmlAttribute
private String name;

my requirement is that name should contain any of the predeifned value a schema similar to this

<xsd:attribute name="type"
        type="simpleType.Generic.ProductReferenceType" use="required" />
<xsd:simpleType name="simpleType.Generic.ProductReferenceType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="OFFER" />
        <xsd:enumeration value="SELLER" />
        <xsd:enumeration value="DEFINITION" />
    </xsd:restriction>
</xsd:simpleType>

i am unable to find out what things i need to do in my class in order to achieve this case

thanks in advance

Was it helpful?

Solution

You can define an enum like this:

@XmlType(name="simpleType.Generic.ProductReferenceType")
public enum ProductReferenceType { 
    OFFER,
    SELLER,
    DEFINITION
}

and then simply use that in your class:

@XmlAttribute
public ProductReferenceType type;

This will generate XSD as follows:

  <xs:simpleType name="simpleType.Generic.ProductReferenceType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="OFFER"/>
      <xs:enumeration value="SELLER"/>
      <xs:enumeration value="DEFINITION"/>
    </xs:restriction>
  </xs:simpleType>

and

    <xs:attribute name="type" type="simpleType.Generic.ProductReferenceType"/>

Good luck in your project!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top