.XML - .XSD如何使用Schema XSD文件引用XML文件?

时间:2014-05-01 13:20:16

标签: xml schema xsd dtd

每次我尝试验证时都说没有分配给doc的DTD或XSD。两个文件都是本地的。

的.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3schools.com"    targetNamespace="http://www.w3schools.com">

<xs:element name="jobList">
 <xs:complexType>
   <xs:sequence minOccurs="0" maxOccurs="unbounded">
     <xs:element ref="job"/>
   </xs:sequence>
 </xs:complexType>
</xs:element>

<xs:element name="job">
 <xs:complexType>
   <xs:sequence>
    <xs:element ref="resource"/>
    <xs:element ref="problem"/>
    <xs:sequence minOccurs="0">
      <xs:element ref="work"/>
    </xs:sequence>
  </xs:sequence>
  <xs:attribute name="status" use="required">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="unallocated"/>
        <xs:enumeration value="allocated"/>
        <xs:enumeration value="completed"/>
        <xs:enumeration value="confirmed"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
</xs:complexType>
</xs:element>

<xs:element name="resource">
<xs:complexType>
  <xs:sequence>
    <xs:element name="manufacturer" type="xs:string"/>
    <xs:element name="model" type="xs:string"/>
    <xs:element name="serialNumber" type="xs:string"/>
  </xs:sequence>
  <xs:attribute name="employeeId" type="xs:string"/>
  <xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>

<xs:element name="problem">
<xs:complexType>
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="reported" type="xs:dateTime" use="required"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
</xs:element>

<xs:element name="work">
<xs:complexType>
  <xs:sequence>
    <xs:element ref="technician"/>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element ref="note"/>
    </xs:sequence>
  </xs:sequence>
  <xs:attribute name="jobId" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:element>

<xs:element name="technician">
<xs:complexType>
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="allocated" type="xs:dateTime" use="required"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="note" type="xs:string"/>
</xs:schema>

和.XML

<?xml version="1.0" encoding="UTF-8"?>

<jobList xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="jobList.xsd">

<job status="unallocated">
    <resource employeeId="AD145267" type="Laptop">
        <manufacturer>Dell</manufacturer>
        <model>Vostro 1540</model>
        <serialNumber>764839211-19-H</serialNumber>
    </resource>
    <problem reported="2012-01-22T15:23:00">Fails to boot</problem>
</job>

<job status="unallocated">
    <resource employeeId="TE135218" type="Wireless printer">
        <manufacturer>Epson</manufacturer>
        <model>Stylus SX435W</model>
        <serialNumber>E4356-982312</serialNumber>
    </resource>
    <problem reported="2012-01-23T10:07:00">No wireless access</problem>
</job>

<job status="allocated">
    <resource type="Network drive">
        <manufacturer>G-Tech</manufacturer>
        <model>G-RAID Hard drive array</model>
        <serialNumber>783451287G</serialNumber>
    </resource>
    <problem reported="2012-01-24T11:02:00">Read errors reported during  rebuild</problem>

    <work jobId="564">
        <technician allocated="2012-01-24T15:05:00">T541</technician>
        <note>Spoke to Eric about what happened.</note>
        <note>Ran phase 1 diagnostics - no errors reported</note>
    </work>
</job>

</jobList>

我还会链接dtd文件(如果你发现它很有用)

<?xml version="1.0" encoding="UTF-8"?>
<!--
TODO define vocabulary identification data
PUBLIC ID  : -//vendor//vocabulary//EN
SYSTEM ID  : http://server/path/__NAME__
-->
<!-- LEVEL 1 (ROOT) ELEMENT  -->
<!ELEMENT jobList (job)*>

<!-- lEVEL 2 ELEMENT -->
<!ELEMENT job (resource, problem, (work)?)>
<!ATTLIST job status (unallocated|allocated|completed|confirmed) #REQUIRED>

<!-- lEVEL 3 ELEMENTS -->
<!ELEMENT resource (manufacturer, model, serialNumber)>
<!ATTLIST resource
employeeId CDATA #IMPLIED
type CDATA #REQUIRED>

<!ELEMENT problem (#PCDATA)>
<!ATTLIST problem reported CDATA #REQUIRED>

<!ELEMENT work (technician, (note)*)>
<!ATTLIST work jobId CDATA #REQUIRED>

<!-- lEVEL 4 ELEMENTS -->
<!ELEMENT manufacturer (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEMENT serialNumber (#PCDATA)>

<!ELEMENT technician (#PCDATA)>
<!ATTLIST technician allocated CDATA #REQUIRED>
<!ELEMENT note (#PCDATA) >

我设法用XML成功验证了DTD(但我删除了引用),但是我很难将XML链接到XSD。提前感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您的架构定义了targetNamespace="http://www.w3schools.com",因此您需要使用noNamespaceSchemaLocation属性而不是schemaLocation来引用XSD。

xsi:schemaLocation =“http://www.w3schools.com jobList.xsd”

需要考虑的事项:

  • 您可以为多个名称空间引用多个XSD;如上所示,为每个人组成一对。

  

根据万维网联盟(W3C)XML Schema   建议,XML实例文档可以同时具备   xsi:schemaLocation和xsi:noNamespaceSchemaLocation属性   指定。此外,您可以多次列出相同的命名空间。

答案 1 :(得分:0)

在您的XML文件中,您有xmlns =&#34; http://www.w3schools.com" 但是在你的XSD文件中你有xmlns =&#34; jobList.xsd&#34;和targetNamespace =&#34; jobList.xsd&#34;

他们需要是相同的