Search code examples
soapwsdlxsd

How to add element in XSD Schema that takes comma separated String values


I am new in XML schema ... I have a xsd schema file :-

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.anirban.com/v1"
    xmlns:tns="http://www.anirban.com/v1" elementFormDefault="qualified">

    <complexType name="InputRequest">
        <sequence>
            <element name="Name" maxOccurs="unbounded" type="string">
            </element>
            <element name="Url" type="anyURI" maxOccurs="1" minOccurs="0">
            </element>

            <element name="Employee">
                <complexType>
                    <sequence>
                        <element name="VehicleList" maxOccurs="unbounded" type="string" />
                        <element name="EmployeeName" type="string" default="Anirban" />

                    </sequence>
                </complexType>
            </element>
        </sequence>
    </complexType>


    <complexType name="OutputResponse">
        <sequence>
            <element name="ResponseCode" type="string"></element>
        </sequence>
    </complexType>

    <element name="getInputRequest" type="tns:InputRequest"></element>

    <element name="getOutputResponse" type="tns:OutputResponse"></element>
</schema>

Which is used to create a SOAP Request like :-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.anirban.com/v1">
   <soapenv:Header/>
   <soapenv:Body>
      <v1:getInputRequest>
         <!--1 or more repetitions:-->
         <v1:Name>Abc</v1:Name>
         <v1:Name>Efg</v1:Name>
         <!--Optional:-->
         <v1:Url>http://localhost:8084/test?wsdl</v1:Url>
         <v1:Employee>
            <!--1 or more repetitions:-->
            <v1:VehicleList>ee</v1:VehicleList>
            <v1:VehicleList>ff</v1:VehicleList>
            <v1:EmployeeName>Anirban</v1:EmployeeName>
         </v1:Employee>
      </v1:getInputRequest>
   </soapenv:Body>
</soapenv:Envelope>

Now my question is, I want to add an element in between like :- <v1:Orders>Ord1,Ord2,Ord3</v1:Orders> which will take String value with comma separated .. So how can I add the the element Orders in between ? Is it possible to add an element which will take comma separated String values in the sequence of complexType ??? Please help ...


Solution

  • As helderdarocha has already pointed out, you would almost certainly do better to specify your element as a sequence of elements:

    <v1:Order>Ord1</v1:Order>
    <v1:Order>Ord2</v1:Order>
    <v1:Order>Ord3</v1:Order>
    

    You can wrap them in a v1:Orders element if you like; different people have different tastes when it comes to things like that.

    If for some reason it really is better to make a single element with a sequence of atomic values (I don't actually believe for a moment that it really will be better, but many people need to learn by painful experience of their own instead of learning from other people's painful experience), then it will work better in XSD and related tools to make the list white-space separated:

    <v1:Orders>Ord1 Ord2 Ord3</v1:Orders>
    

    That way, the Orders element can be declared as a list of simple values, and the individual values can be tested against a type (integer, date, IDREF, NCName, regex-defined token of your choice, ...). The XSD validator, and schema-aware software downstream, will see a sequence of simple values, not one.

    Making it a comma-delimited list is, from an XSD point of view, the worst of all possible solutions. XSD list types are whitespace-delimited, not comma-delimited (and not arbitrary-regex-delimited, no matter how much the Perl aficionados of the world might have liked that), so neither the XSD validator nor downstream schema-aware applications will see a sequence of values; they'll see just one value, with a lexical space constrained by a long complicated (and possibly faulty, unless you're better than the average programmer) regular expression. If the individual tokens are supposed to represent integers, dates, or some other XSD simple type, you get no type validation for them. If you want to restrict the length of the list, it's possible only by means of the regex used to validate the string; another opportunity for error.

    If on the other hand the individual tokens have no lexical constraints, and you really really insist on having a comma-delimited list and not a whitespace-delimited list, you have no work to do at all: just declare the Orders element as xsd:string. Since XSD won't do anything special with the commas, there is no need for a pattern that does anything clever with commas; since the tokens have no lexical constraints, there is no work for a pattern to do in any case.