Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML Schemas

 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am learning xml schemas.

I have created a schema as follows pen.xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs="abcd"
targetNamespace="efgh"
xmlns="hello"
elementFormDefault="qualified">

<xs:element name="pen">
<xs:complexType>
<xs:sequence>
<xs:element name="color" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>


The xml document pen.xml is as follows:

<?xml version="1.0"?>

<note xmlns="hello"
xmlns:xsi="good"
xsi:schemaLocation="yes pen.xsd">

<dear>Tove</dear>
</note>

Now if I open this xml in Internet Explorer is should is throw an error since it expects an element called color in the xml.

But that does not happen.
How do I apply the xml schema to the xml and get it tested if its is working.
Is there anything wrong with my xml or my xsd.

Priety.
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's your schema. A complex type definition should look like this:


I noticed you used xs: but it should have been xsd: and you got an extra later of element, which is redudant to define complex types.

Hope this helps.
 
Paul Clapham
Sheriff
Posts: 21142
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might indeed be problems with the schema -- but I'm not a schema expert. I do know that browsers won't validate XML when they display it, though.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Now I have changed things in my xml and my schema:

This is the new ranch.xml
It has a single element called dear
It mentions ranch.xsd as the schema for its validation:

<?xml version="1.0"?>

<note xmlns="hello"
xmlns:xsi="good"
xsi:schemaLocation="yes ranch.xsd">

<dear>Tove</dear>
</note>


This is the schema ranch.xsd

<?xml version="1.0"?>
<xsd:schema xmlns:xs="abcd"
targetNamespace="efgh"
xmlns="hello"
elementFormDefault="qualified">

<xsd:complexType name="pen">
<xsd:sequence>
<xsd:element name="color" type="colorType" />
</xsd:sequence>
</xsd:complexType>


</xsd:schema>

Now when I open the ranch.xml in Internet Explore I expect it to be validated agains the ranch.xsd schema and the browser should show an error, but it does not happen.
Then what is the use of mentioning the schema in the xml if the browser is not validation against it.

Priety.
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a few xml files and they don't get validated when it is loaded. However, there is a validator online I just found http://tools.decisionsoft.com/schemaValidate/ that will validate your file against your schema. I ran my files through it and guess what... my schemas were invalid. Then I "make" them valid.

I'm sure if you use DTD then when the xml file is loaded in the browser, it will prompt error if your DTD doesn't match.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot K. Tsang for the online validator link.

Yes as you said it does work.

Priety.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic