tags:

views:

3928

answers:

6

I would like to create XML Schema for this chunk of xml, I would like to restrict the values of "name" attribute, so that in output document on and only one instance of day is allowed for each week day:

<a>
  <day name="monday" />
  <day name="tuesday" />
  <day name="wednesday" />
</a>

I have tried to use this:

 <xs:complexType name="a">
  <xs:sequence>
    <xs:element name="day" minOccurs="1" maxOccurs="1">
      <xs:complexType>
        <xs:attribute name="name" use="required">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="monday" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:complexType>
    </xs:element>
    <xs:element name="day" minOccurs="1" maxOccurs="1">
      <xs:complexType>
        <xs:attribute name="name" use="required">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="tuesday" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

but XML Schema validator in eclipse says error "Multiple elements with name 'day', with different types, appear in the model group.".

Is there any other way?

A: 

Try this:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
<xs:element name="a">
 <xs:complexType>
  <xs:sequence>
   <xs:element maxOccurs="unbounded" name="day">
    <xs:complexType>
     <xs:attribute name="atrib" type="xs:string" use="required" />
    </xs:complexType>
   </xs:element>
  </xs:sequence>
 </xs:complexType>
</xs:element>

I think the maxOccurs="unbounded" is what you need.

Micah
I wanted to restrict also values of attribute, I have clarified the question accordingly.
David Skyba
A: 

Have you considered something like this? (i.e. day is a type, and the elements are named after the days of the week)

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:complexType name="day" />
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="monday" maxOccurs="1" minOccurs="0" type="day" />
        <xs:element name="tuesday" maxOccurs="1" minOccurs="0" type="day" />
        <xs:element name="wednesday" maxOccurs="1" minOccurs="0" type="day" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Cyphus
Yes I did, actually I use exactly this right now, but the questioned xml desing seemed to me as more appropriate. Thanks anyway.
David Skyba
A: 

You need something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="day"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="day">
    <xs:complexType>
      <xs:attribute name="name" use="required">
        <xs:simpleType>
         <xs:restriction base="xs:string">
          <xs:enumeration value="monday"/>
          <xs:enumeration value="tuesday"/>
          <xs:enumeration value="wednesday"/>
         </xs:restriction>
        </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>
Jose, this did the trick, thank you for your help.
David Skyba
No, this does not do the trick. If the goal is to allow one-and-only-one element for each day name, this schema won't do it. You need to use xs:unique to formally stipulate the each-day-at-most-once constraint.
Cheeso
A: 

Use choice instead of sequence

By that way u can have only day element one's in a element

And for name attribute use ref attribute and reference it with enumeration

A: 

Dear All,

In order to implement the below SOAP message structure, I have decided to add SOAP header explicitly in WSDL.

In my wsdl i am not able to differentiate between the call to be made to create SOAP request and SOAP response 'authentication' tag since element has same name but different structure.

            **<!-- SOAP Request Header -->**
            <xsd:element name="Authentication">
                    <xsd:complexType>
                            <xsd:sequence>
                                    <xsd:element minOccurs="1" maxOccurs="1"                         name="userid" type="xsd:string"/> 
                                    <xsd:element minOccurs="1" maxOccurs="1" name="password" type="xsd:string"/>                                        
                            </xsd:sequence>
                    </xsd:complexType>
        </xsd:element>

        **<!-- SOAP Response Header -->**
        <xsd:element name="Authentication">
                    <xsd:complexType>
                            <xsd:sequence>
                                    <xsd:element minOccurs="1" maxOccurs="1" name="userid" type="wsp:string"/>
                                    <xsd:element minOccurs="1" maxOccurs="1" name="password" type="wsp:string"/>
                                    <xs:element minOccurs="1" maxOccurs="1" name="payloadVersion" type="wsp:string"/>                                        
                            </xsd:sequence>
                    </xsd:complexType>
        </xsd:element>
            </xsd:schema>

SOAP Request -

    <S:Header>
         <authentication>
               <userid>UserName</userid>
               <password>Password</password>
        </authentication>
    </S:Header>


SOAP response -

    <S:Header>
         <wsp:authentication>
                 <wsp:userid>XXUserName</wsp:userid>
                 <wsp:password>XXPassword</wsp:password>
                 <wsp:payloadVersion>2007B</wsp:payloadVersion>
        </wsp:authentication>
    </S:Header>

I am using Axis1.4, Java 1.5 and Tomcat 5.0 for exposing services.Please advise?

annonymous
+1  A: 

In order to satisfy the at-most-once constraint described in the original question, you need to use a xs:unique element in your schema.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="day" maxOccurs="7" minOccurs="1">
          <xs:complexType>
            <xs:attribute name="name" use="required">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:pattern value="(mon|tues|wednes|thurs|fri|satur|sun)day"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
          </xs:complexType>
          <xs:unique name="eachDayAtMostOnce">
            <xs:selector xpath="day"/>
            <xs:field xpath="@name"/>
          </xs:unique>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>
Cheeso