tags:

views:

21

answers:

1

Hi all,

I want to see xml file in browser as I define in .xsd file. Please check the following two files for me and point out what do I need to do. These two files are under same folder.

employee.xml

 <?xml version="1.0"?>

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

  <firstname>John</firstname>
  <lastname>Smith</lastname>
</employee>

employee.xsd

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string" fixed="red" />
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
+2  A: 

You made two errors: one in the schema file and another in the syntax of the value of the xsi:schemaLocation attribute of the XML file.

The main error is that your employee.xsd file is only a fragment of the XML Schema. You should complete the contain of the employee.xsd. For example,

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.w3schools.com/RedsDevils"
    elementFormDefault="qualified"
    xmlns="http://www.w3schools.com/RedsDevils employee.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

    <xs:element name="employee">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="firstname" type="xs:string" fixed="red" />
                <xs:element name="lastname" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

and employee.xml:

<?xml version="1.0" encoding="utf-8"?>
<employee xmlns="http://www.w3schools.com/RedsDevils"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.w3schools.com/RedsDevils employee.xsd">

    <firstname>John</firstname>
    <lastname>Smith</lastname>
</employee>

Because you define default namespace in the XML file, the schema location attribule xsi:schemaLocation must consist from the the namespace and the path to the schema devided with the blank. I changed the namespace name so that it will be a little more unique: "http://www.w3schools.com/RedsDevils" instead of "http://www.w3schools.com".

At the end I can add that the XML file employee.xml not corresponds to the schema employee.xsd because the element <firstname>John</firstname> has the value other as red, but probably exactly this you wanted to test.

Oleg
thanks you so much!
RedsDevils