2016-08-20 4 views
1

Ich lese Buch "Java XML und JSON" von Apress. Ich erstellte Datei recipe.xml aber nicht gültig:Fehler: Externe Ressource http://www.example.com/ ist nicht registriert

<?xml version="1.0"?> 
<recipe xmlns="http://www.tutortutor.ca/" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.tutortutor.ca/schemas recipe.xsd"> 
    <title>Grilled Cheese Sandwich</title> 
    <ingredients> 
     <ingredient qty="2">bread slice</ingredient> 
     <ingredient>cheese slice</ingredient> 
     <ingredient qty="2">margarine pat</ingredient> 
    </ingredients> 
    <instructions> 
     Place frying pan on element and select medium heat. For each bread 
     slice, smear one pat of margarine on one side of bread slice. Place 
     cheese slice between bread slices with margarine-smeared sides away 
     from the cheese. Place sandwich in frying pan with one 
     margarine-smeared side in contact with pan. Fry for a couple of 
     minutes and flip. Fry other side for a minute and serve. 
    </instructions> 
</recipe> 


recipe.xsd

<?xml version="1.0" encoding="UTF-8" ?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="title" type="xs:string"/> 
    <xs:element name="instructions" type="xs:string"/> 
    <xs:attribute name="qty" type="xs:unsignedInt" default="1"/> 
    <xs:element name="recipe"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="title"/> 
       <xs:element ref="ingredients"/> 
       <xs:element ref="instructions"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="ingredients"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="ingredients" maxOccurs="unbounded"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="ingredient"> 
     <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="xs:string"> 
        <xs:attribute ref="qty"/> 
       </xs:extension> 
      </xs:simpleContent> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

enter image description here Wie ich meine XML-Datei werden gültig zu machen?

Antwort

1

Nehmen Sie folgende Änderungen:

  1. xmlns="http://www.tutortutor.ca/" aus dem Wurzelelement entfernen, da die XSD keine solche targetNamespace hat.
  2. ändern

    xsi:schemaLocation="http://www.tutortutor.ca/schemas recipe.xsd" 
    

    zu

    xsi:noNamespaceSchemaLocation="recipe.xsd" 
    
  3. Befestigen Sie den Fehler im XSD:

    <xs:element ref="ingredients" maxOccurs="unbounded"/> 
    

    zu

    <xs:element ref="ingredient" maxOccurs="unbounded"/> 
    

Dann ist Ihr XML wie angefordert für Ihre XSD gültig.

Verwandte Themen