0

I created the following schema to validate my xml document:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="idType">
    <xs:restriction base="xs:ID">
        <xs:pattern value="IT\d{2}-\d{3}-\d{3}"/>
    </xs:restriction>
</xs:simpleType>
<xs:simpleType name="codeType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="MP"/>
        <xs:enumeration value="SP"/>
         <xs:enumeration value="WPA"/>
    </xs:restriction>
 </xs:simpleType>
 <xs:element name="degrees">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="degree" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
     </xs:complexType>
</xs:element>
 <xs:attribute name="degreeID" type="idType"/>
 <xs:attribute name="degreeCode" type="codeType"/>
 <xs:element name="title" type="xs:string"/>
 <xs:element name="approvalDate" type="xs:date"/>
 <xs:element name="effectiveDate" type="xs:date"/>
 <xs:element name="summary" type="xs:string"/>
<xs:element name="coordinator" type="xs:string"/>
<xs:element name="comment">
     <xs:complexType>
        <xs:simpleContent>
             <xs:extension base="xs:string">
                 <xs:attribute name="date" type="xs:date" use="required"/>
             </xs:extension>
        </xs:simpleContent>
     </xs:complexType>
</xs:element>
<xs:element name="degree">
    <xs:complexType>
         <xs:sequence>
             <xs:element ref="title"/>
            <xs:element ref="approvalDate" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="effectiveDate"/>
            <xs:element ref="summary"/>
            <xs:element ref="coordinator"/>
             <xs:element ref="comment" minOccurs="0" maxOccurs="unbounded"/>
         </xs:sequence>
        <xs:attribute name="degreeID" use="required"/>
        <xs:attribute name="degreeCode" use="required"/>
    </xs:complexType>
</xs:element>

key to the requirement is the sequence which I believe requires the elements to be in that order and that each element occur one time unless otherwise noted.

My schema and XML are well formed and the XML (below) validates against the schema per XMLSpy.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<degrees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="degrees.xsd">
   <degree degreeID="IT10-152-100" degreeCode="MP">
      <title>Information Technology-Mobile Programmer</title>
      <approvalDate>2017-01-12</approvalDate>
      <effectiveDate>2017-09-01</effectiveDate>
      <summary>This two-year program meets the specific skill and knowledge requirements
    of technical and professional jobs within the information technology field for
    an entry-level mobile programmer working in a small to medium size
    organization. Training blends general educational development with required
    IT technical skills. Emphasis is in mobile web and application development.
      </summary>
      <coordinator>Janis Wu</coordinator>
      <comment date="2017-01-12">Janis Wu contacted regards heads up on approval.  Official paperwork is being sent</comment>
      <comment date="2017-01-01">Janis Wu assigned as coordinator</comment>
      <comment date="2016-12-01">Application draft submitted</comment>
   </degree>

</degrees>

But when I manipulate the XML (add and additional approval or effective date element, add a comment in between the date elements) the XML still validates. But I don't understand why. I know the XSD reference is correct, because if I remove the required date attribute from a comment it does not validate.

spike
  • 19
  • 1
  • 6
  • You are using a local xsd. There is a problem on how referrence it. Check this http://stackoverflow.com/questions/19253402/how-to-reference-a-local-xml-schema-file-correctly – Alkis Kalogeris Jul 25 '14 at 13:32
  • The degree element it's not declared to be of any type, so it is correct just by being a degree tag. You could have more effectiveDate element inside the degree element as well as you could also have any attribute or any element child you want in your degree element and it will still being correct. – sergioFC Jul 25 '14 at 13:54

1 Answers1

0

In your XML Schema you are declaring a sequence of elements called degree. Youd don't declare the element called degree to be of any type, so by default it's correct being of any complex type, so you could also have any random attributes or childs in the degree elements of your XML degree element and it will still validate.

It seems like what you really want its something like this

<xs:element name="degrees">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="degree" type="DEGREE_TYPE" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
     </xs:complexType>
</xs:element>

And also change

<xs:element name="degree">
    <xs:complexType>
     etc
    </xs:complexType>
</xs:element>

for:

<xs:complexType name="DEGREE_TYPE">
 etc
</xs:complexType>
sergioFC
  • 5,926
  • 3
  • 40
  • 54