4

I need to declare a list in my xsd file . Not sure what changes do I need to make. I tried using simple type but the required type is not getting generated.

I need my pojo to have the datatype as -

  @XmlElement(name = "Journeys", required = true)
        protected List<LoyaltyJourneyIdentifier> journeys = new ArrayList<LoyaltyJourneyIdentifier>();

My current xsd

                    <xs:complexType name="AccountLoyaltyDetail">
            <xs:sequence>
                <xs:element name="OperatingCompany" type="ns:OperatingCompanyType"
                    minOccurs="0" />
                <xs:element name="Journeys" type="ns:LoyaltyJourneyIdentifier" />
                <xs:element name="Segments" type="ns:LoyaltySegmentIdentifier" />
            </xs:sequence>
</xs:complexType>

<xs:complexType name="LoyaltyJourneyIdentifier">
            <xs:sequence>
                  <xs:element name="JourneyIdentifierId" type="xs:string" maxOccurs="unbounded">
                        <xs:annotation>
                              <xs:documentation>Free form text to be echoed back in the reply.
                                    Used to match requests and replies.</xs:documentation>
                        </xs:annotation>
                  </xs:element>
            </xs:sequence>
</xs:complexType

> currently the pojo is generated as -

@XmlElement(name = "Journeys", required = true)
    protected LoyaltyJourneyIdentifier journeys;

Please let me know what changes do I need to make

themaster
  • 453
  • 11
  • 32

2 Answers2

2

To represent a List in XSD use complexType, not xs:list. Your XSD needs change as follows.

<xs:complexType name="AccountLoyaltyDetail">
            <xs:sequence>
                <xs:element name="OperatingCompany" type="ns:OperatingCompanyType"
                    minOccurs="0" />
                <xs:element name="Journeys" type="LoyaltyJourneyIdentifier" />
                <xs:element name="Segments" type="LoyaltySegmentIdentifier" />
            </xs:sequence>
</xs:complexType>

<xs:complexType name="LoyaltyJourneyIdentifier">
            <xs:sequence>
                  <xs:element name="JourneyIdentifierId" type="xs:string" minOccurs="1" maxOccurs="unbounded">
                        <xs:annotation>
                              <xs:documentation>Free form text to be echoed back in the reply.
                                    Used to match requests and replies.</xs:documentation>
                        </xs:annotation>
                  </xs:element>
            </xs:sequence>
</xs:complexType>

Let's see the difference between xs:list and xs:complexType now. The purpose of xs:list element is not to represent a Collection type, It only lists the similar type of elements as follows.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="stringvalues" type="valuelist"/>

<xs:simpleType name="valuelist">
   <xs:list itemType="xs:string"/>
</xs:simpleType>

</xs:schema>

The "stringvalues" element in a document could look like this (notice that the list will have four list items):

<stringvalues>I love XML Schema</stringvalues> 

Now, To represent the java.util.List in XSD, use below example. List is just a complex type with maxOccurs attribute value as unbounded.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">       
    <xsd:element name="customer">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="phone-number" type="xsd:string" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

I hope this helps.

  • Thanks ... But when I am trying changing my xsd as suggested .I am getting the pojo as provided above not like what I need . Also , I want my list to be of object type .can you tell me what change do I have do for that – themaster May 18 '16 at 12:25
  • I am getting error as " It was detected that 'LoyaltyJourneyIdentifier' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///D:/ILoyalty_WS/Loyalty_WS/src/main/config/properties/contract/xsds/SxgyService.xsd'. If 'LoyaltyJourneyIdentifier' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'LoyaltyJourneyIdentifier' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:///D:/ILoyalty_WS/Loyalty_WS/src/main/config/properties/contract/xsds/ – themaster May 18 '16 at 13:13
  • @themaster: See [this answer](https://stackoverflow.com/a/32906291/983430) for how to fix that error. Basically, you just need to reference your current target namespace. – Amos M. Carpenter Jan 27 '21 at 02:27
0

Following worked for me, you can create from wsdl as well as xsd

Following is the object you want list of

<xs:complexType name="objectClass">
        <xs:sequence>
            <xs:element name="name" type="xs:string" />
            <xs:element name="city" type="xs:string" />
                nillable="true" />
        </xs:sequence>
    </xs:complexType>

Refer this object in other adding min occurrence and max like following

        <xs:sequence>
            <xs:element name="item"
                type="tns:objectClass" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>

Above would result in java like following

public class MainClassWhereYouWantListofAboveClass {
       protected List<objectClass> item;
       //.....
}
Pooja Burande
  • 91
  • 1
  • 5