2014-01-31 3 views
0

Я пытаюсь создать схему для файла XML, но продолжаю получать ошибки проверки. У меня был большой опыт работы с XML, XSLT и DTD, но когда меня попросили создать XSD-файл, я подумал, что смогу решить эту задачу. Меня попросили, чтобы создать схему со следующими характеристиками:указанная схема XSD для вложенных XML-узлов

<payroll> 
     <employee>+ 
     <name> 
      <first>: string of upper/lowercase letters, spaces, or hyphens (-). 
        string must start with an uppercase letter 
      <middle>?: upper case letter 
      <last>: string of upper/lowercase letters, spaces, or hyphens (-). 
        string must start with an uppercase letter 
     <spouse>? –- first, middle, last are the same type as for employee 
      <first> 
      <middle>? 
      <last> 
     <child>* -- first, middle, last are the same type as for employee 
      <first> 
      <middle>? 
      <last> 
     <tax-status> married | single | headOfHousehold | separated 
     <ssn>: A nine digit number of the form ddd-dd-dddd (e.g, 865-57-2934) 
      ssn attribute: name=type; values=(assigned | original); default="original" 
     <salary>: A 9 digit number of the form ddddddd.dd with a minimum value of 
        0 and a maximum value of 2000000, inclusive. 
     <date-of-birth>: A date type 
     <manager> | <staff> 
      <manager> 
      attribute for manager: name=title; type=string; use=required 
      <department>: string 
      <yrsAtRank>: an inclusive integer between 0 and 50 
      <staff> 
      <skill>+: up to 5 skills, each being a string 

Вот XML-файл:

<?xml version = "1.0"?> 

    <employeelist:payroll xmlns:employeelist = "urn:myURN:employeelist"> 
    <employee> 
     <name> 
     <first>Brad</first> 
     <middle>T</middle> 
     <last>Vander Zanden</last> 
     </name> 
     <spouse> 
      <first>Yifan</first> 
      <last>Tang</last> 
     </spouse> 
     <tax-status>married</tax-status> 
     <ssn type="assigned">186-39-3069</ssn> 
     <salary>110000.00</salary> 
     <date-of-birth>1964-02-03</date-of-birth> 
     <manager title="professor"> 
     <department>Computer Science</department> 
     <yrsAtRank>8</yrsAtRank> 
     </manager> 
    </employee> 

    <employee> 
     <name> 
     <first>Don</first> 
     <last>Juan</last> 
     </name> 
     <child> 
     <first>Mary</first> 
     <middle>H</middle> 
     <last>Lamb</last> 
     </child> 
     <child> 
     <first>Fred</first> 
     <last>Flintstone</last> 
     </child> 
     <tax-status>headOfHousehold</tax-status> 
     <ssn>586-38-3969</ssn> 
     <salary>1553.83</salary> 
     <date-of-birth>1980-07-07</date-of-birth> 
     <staff> 
     <skill>Carpentry</skill> 
     <skill>Welding</skill> 
     <skill>Plumbing</skill> 
     </staff> 
    </employee> 
    </employeelist:payroll> 

И .. вот XSD (ниже). Я не могу найти проблему. Валидатор W3C просто говорит: «Разметка плохо сформирована». Любые подсказки или мысли? Заранее спасибо!

<?xml version = "1.0"?> 

<schema xmlns = "http://www.w3.org/2001/XMLSchema" 
    xmlns:employeelist = "urn:csc420:employeelist" 
    targetNamespace = "urn:csc420:employeelist"> 

    <complexType name="employeeType"> 
     <sequence> 
      <element name="employee" type="employeelist:singleEmployeeType" 
       minOccurs="1" maxOccurs="unbounded"/> 
     </sequence> 
    </complexType> 

    <complexType name="nameType"> 
     <sequence> 
      <element name="first" type="string"/> 
      <element name="middle" type="string"/> 
      <element name="last" type="string"/> 
     </sequence> 
    </complexType> 

    <complexType name="spouseType"> 
     <sequence> 
      <element name="first" type="string"/> 
      <element name="middle" type="string"/> 
      <element name="last" type="string"/> 
     </sequence> 
    </complexType> 

    <simpleType name="tax-statusType"> 
      <restriction base = "string"> 
       <enumeration value = "married"/> 
       <enumeration value = "single"/> 
       <enumeration value = "headOfHousehold"/> 
       <enumeration value = "separated"/> 
      </restriction> 
    </simpleType> 

    <simpleType name="ssnType"> 
     <restriction base = "int"> 
      <pattern value = "[0-9]{3}\-[0-9]{2}\-[0-9]{4}"/> 
     </restriction> 
    </simpleType> 

    <simpleType name="salaryType"> 
     <restriction base="decimal"> 
      <minInclusive value="0.00"/> 
      <maxInclusive value="200000000.00"/> 
     </restriction> 
    </simpleType> 

    <simpleType name="date-of-birthType"> 
     <restriction base="decimal"> 
      <minInclusive value = "0"/> 
      <maxInclusive value = "10"/> 
     </restriction> 
    </simpleType> 

    <simpleType name="skillType"> 
     <restriction base="decimal"> 
      <minInclusive value = "1"/> 
      <maxInclusive value="5"/> 
     </restriction> 
    </simpleType> 

    <complexType name="managerType"> 
     <sequence> 
      <element name="department" type="string"/> 
      <element name="yrsAtRank" type="int"/> 
     </sequence> 
     <attribute name="title" type="string"/> 
    </complexType> 

    <complexType name="staffType"> 
     <sequence> 
      <element name="skill" type="employeelist:skillType"/> 
     </sequence> 
    </complexType> 

    <complexType name="singleEmployeeType"> 
     <sequence> 
      <element name="name" type="employeelist:nameType"/> 
      <element name="spouse" type="employeelist:spouseType"/> 
      <element name="tax-status" type="employeelist:tax-statusType"/> 
      <element name="ssn" type="employeelist:ssnType"/> 
      <element name="salary" type="employeelist:salaryType"/> 
      <element name="date-of-birth" type="date"/> 
      <element name="manager" type="employeelist:managerType"/> 
      <element name="staff" type="employeelist:staffType"/> 
     </sequence> 
    </complexType> 

    <element name="payroll" type="employeelist:singleEmployeeType"/> 

</schema> 
+0

Даже если вы смущены, вы должны предоставить то, что у вас есть. StackOverflow не является бесплатной службой XSD-записи. – JLRishe

+0

@JLRishe JL, я добавил свой код, вы на первый взгляд видите какие-либо синтаксические или логические проблемы? Благодарю. – Jim22150

+0

Ваш XSD в настоящее время недействителен по трем причинам: (1) У вас есть пробел между 'xmlns:' и 'employeelist'. (2) У вас есть 'minInclusive' и' maxInclusive' непосредственно внутри элемента 'simpleType', но они должны содержаться в элементе' ограничения'. (3) В вашем последнем complexType вы используете префикс 'employeeList' (капитал L), но префикс пространства имен, который вы указали в верхней части, является' employeelist' (нижний регистр L). – JLRishe

ответ

1

Вы определили тип для «заработной платы», чтобы быть «singleEmployeeType», так что ожидает, что элементы одного работника (name, spouse и т.д.) непосредственно внутри <payroll> элемента. Это не соответствует вашему входному XML, и он не выглядит так, как вы его надумали.

Просто измените определение payroll элемента в вашей XSD для:

<element name="payroll" type="employeelist:employeeType"/> 

В вашей XSD, вы определили employeeType быть 1 или более <employee> элементов. Я предлагаю вам исправить имена типов в XSD, чтобы они соответствовали тому, принимает ли он только одно или несколько из них. Какое бы ни было соглашение, которое вам нравится, это хорошо, если оно последовательное. Вы можете переименовать employeeType в employeesType или employeeListType, если вы используете соглашение «множественное число» или «добавить список» последовательно.

Смежные вопросы