3

I'm a beginner. Here is my XML Schema code :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.w3.org/1999/xhtml">
    <xs:element name="breaksfast_menu">

        <xs:complexType>
            <xs:sequence>
                <xs:element name="food">
                    <xs:complexType>
                        <xs:sequence>
                         <xs:element name="name" type="xs:string"/>
                         <xs:element name="price" type="xs:string"/>
                         <xs:element name="description" type="xs:string"/>
                         <xs:element name="calories" type="xs:string"/>

                        </xs:sequence>
                    </xs:complexType>
                </xs:element>

            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The XML document:

    <?xml version="1.0" encoding="UTF-8"?>
    <breakfast_menu xmlns:xsi="http://www.w3.org/1999/xhtml">

        <food>
            <name>Belgian Waffles</name>
            <price>$5.95</price>
            <description>
                Two of our famous Belgian Waffles with plenty of real maple syrup
            </description>
            <calories>650</calories>
        </food>

  </breakfast_menu>

Upon trying to validate the XML file, I get an error that says:

The prefix xs for element xs:schema is not bound.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
FF FF
  • 31
  • 1
  • 2

1 Answers1

3

Correction of initial error

The prefix xs for element xs:schema is not bound.

In any XML document that uses namespaces, including an XSD, all used namespace prefixes (e.g. xs) must be bound to a namespace URI (e.g. http://www.w3.org/2001/XMLSchema) in order for the XML document to be namespace-well-formed.

Furthermore, XSD elements are defined in a particular namespace: http://www.w3.org/2001/XMLSchema (not http://www.w3.org/1999/xhtml, which is the namespace for XHTML, not XSD).

Therefore, change

xmlns="http://www.w3.org/1999/xhtml"

to

xmlns:xs="http://www.w3.org/2001/XMLSchema"

to eliminate your initial error.

Correction of aditional errors

After making the above fix, a couple additional issues remain to resolve.

In your XSD

  1. breaksfast_menu should be breakfast_menu to match your XML;
  2. xmlns:xsi="http://www.w3.org/1999/xhtml" is unnecessary, but should you need it to define, say, xsi:noNamespaceSchemaLocation, you'll need to correct it to be xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance".

After making all of the above changes, your XML will be valid against your XSD.


Since you're still having some trouble, here are the corrected XML and XSD. Hope this helps:

XML

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="try.xsd">
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
      Two of our famous Belgian Waffles with plenty of real maple syrup
    </description>
    <calories>650</calories>
  </food>
</breakfast_menu>

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="breakfast_menu">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="food">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="price" type="xs:string"/>
              <xs:element name="description" type="xs:string"/>
              <xs:element name="calories" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I have tried , I have done modifications but i receive a new error (Invalid content was found starting with element 'food'. No child element is expected at this point.). Below you will find the modifications: (XML document) , (XML Schema) – FF FF Apr 20 '18 at 12:57
  • 1
    My guess is that you have defined more than one food in your breakfast menu, but your schema only allows one. However, this is a new problem so it should be a new question. And: I would recommend doing some reading before you do any more coding. Asking SO questions when you hit a problem takes much longer than looking up the answer in a book. – Michael Kay Apr 20 '18 at 13:26
  • @FFFF: I agree with Michael Kay's points and will add that the exact XML in your question is valid against the exact XSD in your question, with the modifications described in this answer. I suggest that you reaffirm this base case before making any further modifications. – kjhughes Apr 20 '18 at 13:34
  • @FFFF: I've added the corrected XML and XSD to the answer so you have a known-valid starting point. Good luck. – kjhughes Apr 20 '18 at 13:43
  • 1
    @FFFF If you encounter a different problem, please accept this answer and ask a new question. Don't pull the answerer in a "please help me with until I'm satisfied" game. – lexicore Apr 20 '18 at 13:46