Pregunta

Every time I try and validate it says No DTD or XSD assigned to doc. Both files are local.

.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>

And the .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>

I will also link the dtd file (incase you find it useful)

<?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) >

I managed to successfully validate the DTD with XML (but I removed the reference), but I'm struggling to get the XML to link to the XSD. Thank you in advance for your help

¿Fue útil?

Solución

Your schema defines targetNamespace="http://www.w3schools.com" and so instead of noNamespaceSchemaLocation you need to reference the XSD using the schemaLocation attribute.

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

Things to consider:

  • you could reference multiple XSDs for multiple namespaces; form a pair for each, as shown above.

And

According to the World Wide Web Consortium (W3C) XML Schema Recommendation, XML instance documents can have both xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes specified. In addition, you can list the same namespace several times.

Otros consejos

In your XML file you have xmlns="http://www.w3schools.com" but in your XSD file you have xmlns="jobList.xsd" and targetNamespace="jobList.xsd"

They need to be the same

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top