Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml : how to reference a .xsd file at .xml file?

Tags:

xml

xsd

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> 
like image 704
RedsDevils Avatar asked Oct 19 '10 04:10

RedsDevils


People also ask

How do you reference in XML?

In XML, character and entity references are formed by surrounding a numerical value or a name with & and ; —for example, &#169; is a decimal character reference and &copy; is an entity reference.

How does XSD work with XML?

XSD is based and written on XML. XSD defines elements and structures that can appear in the document, while XML does not. XSD ensures that the data is properly interpreted, while XML does not. An XSD document is validated as XML, but the opposite may not always be true.

Which attribute is used to reference an XML schema in an XML document?

The 'noNamespaceSchemaLocation' attribute is used to reference XML Schema(s) that are not defined in a target-namespace.


1 Answers

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

like image 52
Oleg Avatar answered Sep 20 '22 06:09

Oleg