0

Hi I have an xml that looks like,

<ABC>
<DEF Value="123">
<XYZ>
<RootEle Text="Now" Date="SomeDate"/>
</XYZ> 
</DEF>
<DEF AnotherValue="123">
<XYZ>
<AnotherRootEle AnotherText="NowOrNever" AnotherDate="SomeOtherDate"/>
</XYZ> 
</DEF>
</ABC>

I need to write an xsd for this. But the xsd that I wrote does not hold good for the above xml.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
   <xsd:element name="ABC">
      <xsd:complexType>
         <xsd:sequence minOccurs="1" maxOccurs="1">
            <xsd:element name="DEF">
               <xsd:complexType>
                  <xsd:sequence minOccurs="1" maxOccurs="unbounded">
                     <xsd:element name="XYZ">
                        <xsd:complexType>
                           <xsd:sequence minOccurs="1" maxOccurs="1">
                              <xsd:element name="RootEle">
                                 <xsd:complexType>
                                    <xsd:attribute name="Text" use="optional">
                                       <xsd:annotation>
                                          <xsd:documentation>Text</xsd:documentation>
                                       </xsd:annotation>
                                       <xsd:simpleType>
                                          <xsd:restriction base="xsd:string">
                                             <xsd:maxLength value="40" />
                                          </xsd:restriction>
                                       </xsd:simpleType>
                                    </xsd:attribute>
                                    <xsd:attribute name="Date" use="optional">
                                       <xsd:annotation>
                                          <xsd:documentation>Date</xsd:documentation>
                                       </xsd:annotation>
                                       <xsd:simpleType>
                                          <xsd:restriction base="xsd:string">
                                             <xsd:maxLength value="40" />
                                          </xsd:restriction>
                                       </xsd:simpleType>
                                    </xsd:attribute>
                                 </xsd:complexType>
                              </xsd:element>
                           </xsd:sequence>
                        </xsd:complexType>
                     </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute name="Value" use="optional">
                     <xsd:annotation>
                        <xsd:documentation>Name of the flow</xsd:documentation>
                     </xsd:annotation>
                     <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                           <xsd:maxLength value="40" />
                        </xsd:restriction>
                     </xsd:simpleType>
                  </xsd:attribute>
                  <xsd:attribute name="AnotherValue" use="optional">
                     <xsd:annotation>
                        <xsd:documentation>Name of interface</xsd:documentation>
                     </xsd:annotation>
                     <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                           <xsd:maxLength value="40" />
                        </xsd:restriction>
                     </xsd:simpleType>
                  </xsd:attribute>
               </xsd:complexType>
            </xsd:element>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>

The above xsd holds good for the below xml but not for the one posted first i.e it is not working for Repeat Elements with different child elements.

<ABC>
    <DEF Value="123">
        <XYZ>
            <RootEle Text="Now" Date="SomeDate"/>
        </XYZ>
    </DEF>
</ABC>

Any help with this ??

SMA_JAVA
  • 471
  • 4
  • 9
  • 18
  • It looks like `DEF` element should have some occurs modification; what you have right now it's saying that a `DEF` can have many `XYZ` childs. – sergioFC Jul 24 '14 at 20:12

1 Answers1

1

sergioFC is correct with his comment you have defined the occurrence as the wrong level

Rather than

        <xsd:element name="DEF">
           <xsd:complexType>
              <xsd:sequence minOccurs="1" maxOccurs="unbounded">

You should have

        <xsd:element minOccurs="1" maxOccurs="unbounded" name="DEF">
           <xsd:complexType>
              <xsd:sequence>

However you also have AnotherRootEle in your XML that does not appear in your XSD at all.

For that you would need to redefine your XYZ with a choice as below.

          <xsd:element name="XYZ">
            <xsd:complexType>
              <xsd:sequence minOccurs="1" maxOccurs="1">
                <xsd:choice minOccurs="0">
                  <xsd:element name="RootEle">
                    <xsd:complexType>
                      <xsd:attribute name="Text" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Text</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                      <xsd:attribute name="Date" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Date</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                    </xsd:complexType>
                  </xsd:element>
                  <xsd:element name="AnotherRootEle">
                    <xsd:complexType>
                      <xsd:attribute name="AnotherText" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Text</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                      <xsd:attribute name="AnotherDate" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Date</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                    </xsd:complexType>
                  </xsd:element>
                </xsd:choice>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54