Search code examples
xmlxsdxsd-validationxml-validation

Where should xsd:attribute go?


I am asked to build a business database and this is my first fictional "company." I have gotten the schema to validate with the XML, but was asked to put in an ORDER/RANKING for at least one company in the XML file. However I try to do this, I can't manage to make it validate (even when provided with the correct line).

This is my XML file (you can see I put ranking="3" and that is the line I can't write into my schema).

   <?xml version="1.0"?>
   <CompanyList> 
  <Company ranking="3">
    <CompanyName>CasesWorldwide</CompanyName>
    <CompanyID>00001</CompanyID>
    <City>Edmonton</City>
    <Province>Alberta</Province>
<ContactName>
    <LastName>Soliman</LastName>
    <FirstName>Ahmed</FirstName>
    <PhoneNumber>646959891</PhoneNumber>
    <ContactCompanyID>00001</ContactCompanyID>
</ContactName>
    <NumberOfEmployees>4000</NumberOfEmployees>
 </Company>
</CompanyList> 

Here is the schema (my attempted line is beside "Company"):

    <?xml version="1.0"?>
 <!-- XSD schema for CS Assignment #3.xml -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="CompanyList">
<xsd:complexType>
<xsd:sequence>

<xsd:element name="Company"> <xsd:attribute name="ranking” type=“xsd:string” use=“optional”/>  
<xsd:complexType> 
<xsd:sequence>    

    <xsd:element name="CompanyName"> 
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
            <xsd:minLength value="1"/>
            <xsd:pattern value=".*[^\s].*" />
        </xsd:restriction>
    </xsd:simpleType>
    </xsd:element>

    <xsd:element name="CompanyID" type="xsd:integer"/>

    <xsd:element name="City" type="xsd:string" minOccurs="0"/>

    <xsd:element name="Province" type="xsd:string" minOccurs="0"/>

    <xsd:element name="ContactName" minOccurs = "1" maxOccurs="5"> 
    <xsd:complexType>
    <xsd:sequence>             

            <xsd:element name="LastName" type="xsd:string" minOccurs="1"/>

            <xsd:element name="FirstName" type="xsd:string" minOccurs="1"/>

            <xsd:element name="PhoneNumber" type="xsd:integer" minOccurs="1"/> 

            <xsd:element name="ContactCompanyID" type="xsd:integer" minOccurs="1"/>

     </xsd:sequence>
    </xsd:complexType>
    </xsd:element>    

    <xsd:element name="NumberOfEmployees" type="xsd:integer" minOccurs="0"/>    


  </xsd:sequence>
</xsd:complexType>
</xsd:element>

    </xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>

Solution

  • In general, xsd:attribute can appear:

    • At the top level of an XSD (within xsd:schema).
    • Within xsd:complexType (completely or by reference).
    • Within an xs:attributeGroup (completely or by reference).

    You are very close; you just need two corrections:

    1. Place your xsd:attribute after xsd:sequence within xsd:complexType.
    2. Change the smart quotes in xsd:attribute to regular quotes.

    Altogether:

    <?xml version="1.0"?>
    <!-- XSD schema for CS Assignment #3.xml -->
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="CompanyList">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Company">
              <xsd:complexType> 
                <xsd:sequence>    
                  <xsd:element name="CompanyName"> 
                    <xsd:simpleType>
                      <xsd:restriction base="xsd:string">
                        <xsd:minLength value="1"/>
                        <xsd:pattern value=".*[^\s].*" />
                      </xsd:restriction>
                    </xsd:simpleType>
                  </xsd:element>
                  <xsd:element name="CompanyID" type="xsd:integer"/>
                  <xsd:element name="City" type="xsd:string" minOccurs="0"/>
                  <xsd:element name="Province" type="xsd:string" minOccurs="0"/>
                  <xsd:element name="ContactName" minOccurs = "1" maxOccurs="5"> 
                    <xsd:complexType>
                      <xsd:sequence>             
                        <xsd:element name="LastName" type="xsd:string" 
                                     minOccurs="1"/>
                        <xsd:element name="FirstName" type="xsd:string"
                                     minOccurs="1"/>
                        <xsd:element name="PhoneNumber" type="xsd:integer" 
                                     minOccurs="1"/> 
                        <xsd:element name="ContactCompanyID" type="xsd:integer" 
                                     minOccurs="1"/>
                      </xsd:sequence>
                    </xsd:complexType>
                  </xsd:element>    
                  <xsd:element name="NumberOfEmployees" type="xsd:integer" 
                               minOccurs="0"/>    
                </xsd:sequence>
                <xsd:attribute name="ranking" type="xsd:string" use="optional"/>  
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    

    This XSD will successfully validate your XML.