XML模式验证项目

时间:2016-10-10 16:43:03

标签: xml

我一直在尝试验证这个与学生和教师打交道的XML文档,并且我一直在收到错误信息,说明该元素无效。我的错误似乎是元素在大多数情况下是无效的。

我将属性声明移到了元素声明的顶部,我又收到了另一个错误。我不能为我的生活弄清楚如何使这个模式工作和验证XML文档。我的XML文档以及我的架构元素都很好。

下面是我的XML文件,以及我的2架构。有3个文档,一个常规文档和两个XML模式。我希望得到一个如何解决这些错误的答案,因为我已尽力解决这些问题。它似乎与我的属性或命名空间有关,因为我在XML文档中声明了2个命名空间。

我之前尝试过验证没有名称空间的XML文档,我似乎对它做得很好。当存在多个命名空间时,我似乎遇到了模式验证的问题,或者我在混合中添加了属性。关于如何避免将来出现这些问题的任何提示也将不胜感激。

XML文档

?xml version="1.0" encoding="UTF-8"?>
<HACC-People xmlns:s="http://www.college.com/ns/student"
                         xmlns:f="http://www.college.com/ns/faculty"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://www.college.com/ns/student Hacc-faculty1.xsd
                                                           http://www.college.com/ns/faculty hacc-faculty2.xsd">
  <s:student>
    <s:name first="Tyler" last="Brown">
      <s:major>Mathematics</s:major>
      <s:advisor>Norman Williams</s:advisor>
      <s:GPA>3.62</s:GPA>
      <s:phone-number>717-358-4780</s:phone-number>
    </s:name>
    <s:name first="Benny" last="Elliott">
      <s:major>Computer Science</s:major>
      <s:advisor>Amy Smith</s:advisor>
      <s:GPA>2.89</s:GPA>
      <s:phone-number>717-867-3125</s:phone-number>
    </s:name>
    <s:name first="Marcus" last="Wilt">
      <s:major>Pyschology</s:major>
      <s:advisor>Alex Schwartz</s:advisor>
      <s:GPA>3.30</s:GPA>
      <s:phone-number>717-225-4982</s:phone-number>
    </s:name>
  </s:student>
  <f:faculty>
    <f:name first="Todd" last="Anderson">
      <f:subject-taught>Web</f:subject-taught>
      <f:courses-taught>Web101, Web110, Web125</f:courses-taught>
      <f:years-teaching>5</f:years-teaching>
      <f:email>todda@hacc.edu</f:email>
      <f:phone-number>717-555-4000</f:phone-number>
    </f:name>
    <f:name first="Mark" last="Calhoun">
      <f:subject-taught>Accounting</f:subject-taught>
      <f:courses-taught>ACCT101, ACCT200, ACCT203</f:courses-taught>
      <f:years-teaching>10</f:years-teaching>
      <f:email>mjcal@hacc.edu</f:email>
      <f:phone-number>717-373-2508</f:phone-number>
    </f:name>
    <f:name first="Elizabeth" last="Hill">
      <f:subject-taught>History</f:subject-taught>
      <f:courses-taught>Hist104, Hist111</f:courses-taught>
      <f:years-teaching>3</f:years-teaching>
      <f:email>lizhill@hacc.edu</f:email>
      <f:phone-number>717-913-6942</f:phone-number>
    </f:name>
  </f:faculty>
</HACC-People>

XML Schema1

<?xml version="1.0" encoding="utf-8"?>
<schema xmlns:s="http://www.college.com/ns/student" 
               attributeFormDefault="unqualified" 
               elementFormDefault="qualified" 
               targetNamespace="http://www.college.com/ns/student" 
               xmlns="http://www.w3.org/2001/XMLSchema">
  <element name="HACC-People">
    <complexType>
      <sequence>
        <element name="student">
        <attribute name="first" type="string" use="required" />
        <attribute name="last" type="string" use="required" />
          <complexType>
             <sequence>
               <element name="name" maxOccurs="unbounded" >
                 <complexType>
                   <sequence>
                     <element name="major" type="string" />
                     <element name="advisor" type="string" />
                     <element name="GPA" type="decimal" />
                     <element name="phone-number" type="string" />
                   </sequence>

               </complexType>
            </element>
          </sequence>
           <element ref="s:student" />
          <element ref="f:faculty" />
        </complexType>
      </element>
     </sequence>
  </complexType>
  </element>
</schema>

XML Schema2

<?xml version="1.0" encoding="utf-8"?>
<schema xmlns:f="http://www.college.com/ns/faculty" 
               attributeFormDefault="unqualified" 
               elementFormDefault="qualified" 
               targetNamespace="http://www.college.com/ns/faculty" 
               xmlns="http://www.w3.org/2001/XMLSchema">
  <import namespace="http://www.college.com/ns/student"
          schemaLocation="Hacc-faculty1.xsd" />
  <element name="faculty">
  <attribute name="first" type="string" use="required" />
  <attribute name="last" type="string" use="required" />
    <complexType>
      <sequence>
        <element name="name" maxOccurs="unbounded">
          <complexType>
            <sequence>
              <element name="subject-taught" type="string" />
              <element name="courses-taught" type="string" />
              <element name="years-teaching" type="integer" />
              <element name="email" type="string" />
              <element name="phone-number" type="string" />
            </sequence>

          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
</schema>

0 个答案:

没有答案