Jump to content

XML Schema basics


Lynx

Recommended Posts

Hi everybody,I am new of this forum and newbie of XML Schema so please forgive my silly question but I really need to understand one thing. I have a schema like in the tutorial: note.xsd

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.w3schools.com"xmlns="http://www.w3schools.com"elementFormDefault="qualified"><xs:element name="note">  <xs:complexType>    <xs:sequence>	  <xs:element name="to" type="xs:string"/>	  <xs:element name="from" type="xs:string"/>	  <xs:element name="heading" type="xs:string"/>	  <xs:element name="body" type="xs:string"/>    </xs:sequence>  </xs:complexType></xs:element></xs:schema>

and an the XML with reference to the XML Schema:

<?xml version="1.0"?><notexmlns="http://www.w3schools.com"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.w3schools.com note.xsd">  <to>Tove</to>  <from>Jani</from>  <heading>Reminder</heading>  <body>Don't forget me this weekend!</body></note>

( Note: note.xml and note.xsd are in the same directory ) I am still learning something about basics rules and I tryed to change something within the XML file in order to make some tests on my local machine.In the example above I tryed to change the parameter of the element "to" like this:

<xs:element name="to" type="xs:string"/>

in

<xs:element name="to" type="xs:integer" />

from string to integer in order to check the behaviour when I read my .xml through browser but seems nothing happen.I also tryed something like this :

<xs:element name="to" type="xs:string" default="No name specified" />

and left the element "to" empty. <to></to>I was expecting to read something like this:

<to>No name specified</to>

when reading my xml but I simply get a tag like this <to/> Maybe the file xsd doesn't affect directly on xml?Where can i find a real working example? Thanks in advance

Link to comment
Share on other sites

It's not that the XSD "doesn't affect directly on xml", it's that browsers don't validate XML files, whether that's with DTD, XML Schema or anything else.To see XML Schema do its thing, you need to give the XML file to an XML Schema validator, which would then tell you what errors are there, if any, in the XML, or the schema itself.There are validators included in many editors that support XML, such as Stylus Studio, triggered by the click of a button.If you want to automatically (via a web application that is) check if an XML file is valid against an XML Schema, there are also APIs in various languages, such as PHP's DOMDocument::schemaValidate() method for example.

  • Like 1
Link to comment
Share on other sites

Thanks Boen for quick reply!Yep! Infact I was reading some articles related with XML Schema validators and I understood that it's not possible to delegate the burden of validation just to the browser. That's why I need PHP libxml or DOMdocument ( exactly DOMDocument::schemaValidate() ) extensions in order to do this. :good:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...