jQuery in Action, 2nd edition*
The moose likes XML and Related Technologies and the fly likes XML Schemas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML Schemas" Watch "XML Schemas" New topic
Author

XML Schemas

Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

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

Joined: Sep 13, 2007
Posts: 2242
    
    7

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 10, 2008
Posts: 156

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

Joined: Sep 13, 2007
Posts: 2242
    
    7

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

Joined: Jun 10, 2008
Posts: 156

Thanks a lot K. Tsang for the online validator link.

Yes as you said it does work.

Priety.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML Schemas
 
Similar Threads
XML / XSL problem
how to validate xml using local xsd
Creating a JMS TextMessage From XML
Jaxb marshalling exception
unexpected child element xs:element error??