How to specify a list of complexType in XML?
Asked Answered
S

2

5

I have XML specified the following:

    <xs:element name="getNewsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="newsItem" type="tns:newsList"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="newsList">
        <xs:list itemType="tns:news"/>
    </xs:simpleType>

   <xs:complexType name="news">
        <xs:sequence>
            <xs:element name="id" type="xs:string"/>
            <xs:element name="date" type="xs:string"/>
            <xs:element name="author" type="tns:author"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="shortDescription" type="xs:string"/>
            <xs:element name="content" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>

I would like to have a list of the news in my response. However when I would like to create Java object with jaxb2 the xml returns the folllowing error when I run mvn clean compile -X:

org.xml.sax.SAXParseException: cos-st-restricts.1.1: The type 'newsList' is atomic, so its {base type definition}, 'tns:news', must be an atomic simple type definition or a built-in primitive datatype.

How I should change my XML to be able to compile?

Selfwinding answered 15/3, 2018 at 12:12 Comment(7)
I've always used maxOccurs="unbounded"Saville
Where exactly should I add that?Selfwinding
As an attribute of newsItem. Check w3.org/TR/xmlschema-0/#ref6 and many other questions here in SO.Saville
thanks for help. I tried all possible ways still not work. Basically the example you provided uses exact same way how i do, also tried the maxOccurs without luck.Selfwinding
Can you post the original error then?Saville
I run maven in debug mode, and it returned actually some meaningful error., which I still can't understand. :)Selfwinding
That error means that you can't use complex types with xs:list, take a look: w3.org/TR/xmlschema-0/#ListDtSaville
S
9

In addition to using the built-in list types, you can create new list types by derivation from existing atomic types. You cannot create list types from existing list types, nor from complex types.

https://www.w3.org/TR/xmlschema-0/#ListDt

This is from one of my working XSD, a user with multiple addresses:

<xs:complexType name="user">
    <xs:sequence>
        <xs:element name="addresses" type="tns:addressData" nillable="true" minOccurs="0" maxOccurs="unbounded"/>

Note that addressData is a complexType.

I guess this is what you need:

<xs:element name="getNewsResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="newsItems" type="tns:news" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="news">
    <xs:sequence>
        <xs:element name="id" type="xs:string"/>
        <xs:element name="date" type="xs:string"/>
        <xs:element name="author" type="tns:author"/>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="shortDescription" type="xs:string"/>
        <xs:element name="content" type="xs:string"/>
    </xs:sequence>
</xs:complexType>
Saville answered 15/3, 2018 at 20:29 Comment(4)
Can you give me an example for "you can create new list types by derivation from existing atomic types." I can see in this example which you shared with me that the USStatelist exactly looks like my newsList.Selfwinding
The problem is with your "news" element, it can't be a complexType if you want to reference it from a xs:listSaville
Ok. Thanks for help. I can see now that USStateList is list of strings. I wonder how it is possible to return a list of object in xml, if a complextypes are not allowed.Selfwinding
Your example is compiled and works. Really nice thank you.Selfwinding
K
0
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"     
           targetNamespace="http://pro.com/balcao/xdto"
            xmlns:tns="http://pro.com/balcao/xdto"
           elementFormDefault="qualified">
    

    <xs:element name="salvarCliente" >
        <xs:complexType>
        <xs:sequence>
            <xs:element name="nome" type="xs:string" />
            <xs:element name="telefone" type="xs:string" />
            <xs:element name="provincia" type="xs:string" />
            <xs:element name="municipio" type="xs:string" />
        </xs:sequence>
        </xs:complexType> 
    </xs:element>



        <xs:complexType name="listarClientes">
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
                <xs:element name="clientes" type="tns:cliente" />
            </xs:sequence>
        </xs:complexType>
 
 
    <xs:complexType name="cliente">
            <xs:sequence>
                <xs:element name="nome" type="xs:string" />
                <xs:element name="telefone" type="xs:string" />
                <xs:element name="provincia" type="xs:string" />
                <xs:element name="municipio" type="xs:string" />
            </xs:sequence>
    </xs:complexType> 
</xs:schema>
Kitti answered 26/11, 2020 at 21:9 Comment(1)
When answering questions it's best to provide some context in addition to code. With just code it's hard for users to understand what your answer is doing. It would be great to include some descriptions of how it solves the issue and why it works.Phonemic

© 2022 - 2024 — McMap. All rights reserved.