Problem handling schema in Xerces 1.4.2

Status
Not open for further replies.

Steve Dussinger

Usenet Archive
Joined
Jul 21, 2001
Messages
9
Reaction score
0
Can anyone tell me just what I'm doing wrong? I've got a simple java program using Xerces 1.4.2 to parse a simple XML file. I want to use schema validation, so I turned on the appropriate features, but no matter what I do I get errors saying that none of my elements are defined. I've tried everything I can think of, but I still can't get it to work.

Here's the code I'm using:

import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.validators.schema.XUtil;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;

public class parseTest implements ErrorHandler
{
public void error(SAXParseException se) throws SAXParseException { System.out.println ("ERROR: " + se.getMessage ); System.out.println("ERROR : Line Number =" + se.getLineNumber); System.out.println("ERROR : Col Number =" + se.getColumnNumber); }

public void warning(SAXParseException se)
{
System.out.println ("WARNING: " + se.getMessage ); }

public void fatalError (SAXParseException se) throws SAXParseException { System.out.println ("FATAL: " + se.getMessage ); }

private void test (String fileName)
{
try {
System.out.println ("Parsing: " + fileName);

DOMParser parser = new DOMParser ;
parser.setFeature(
"http://apache.org/xml/features/dom/defer-node-expansion", true); parser.setFeature( "http://xml.org/sax/features/validation", true ); parser.setFeature( "http://xml.org/sax/features/namespaces", true ); parser.setFeature( "http://apache.org/xml/features/validation/schema", true ); parser.setFeature( "http://apache.org/xml/features/dom/include-ignorable-whitespace", false);

parser.setErrorHandler (this);

parser.parse (new InputSource (new FileInputStream (fileName)));

} catch (Exception e) {
e.printStackTrace ;
}
}

public static void main (String args)
{
new parseTest .test (args[0]);
}
}

And the XML:

<?xml version="1.0" ?>

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="schemaTest.xsd">
<row>
<product>Product</product>
</row>
</root>

And the XSD:

<?xml version ="1.0"?>
<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema">

<xsd:complexType name="rowType" content = "elementOnly"> <xsd:sequence> <xsd:element name = "product" type="string"/> </xsd:sequence> </xsd:complexType>

<xsd:complexType name="rootType" content = "elementOnly"> <xsd:sequence> <xsd:element name = "row" type="rowType"/> </xsd:sequence> </xsd:complexType>

<xsd:element name = "root" type="rootType"/>

</xsd:schema>

When I run the Java program, I get the following:

ERROR: Element type "root" must be declared.
ERROR : Line Number =3
ERROR : Col Number =100
ERROR: Element type "row" must be declared.
ERROR : Line Number =4
ERROR : Col Number =8
ERROR: Element type "product" must be declared.
ERROR : Line Number =5
ERROR : Col Number =14

These errors come from my error handler code, so I know that it's installed correctly...

I've tried changing the xsi:noNameSpaceSchemaLocation to point to a file which doesn't exist, expecting some kind of error telling me that Xerces can't find the file. No such luck, it keeps giving me the same errors. I'm convinced that Xerces isn't even trying to look at the schema file, but I have no idea why. Does anyone out there have a clue what I'm doing wrong??

Any help is greatly appreciated...

Thanx,
Steve
 

Frank Weyand

Usenet Archive
Joined
Aug 13, 2001
Messages
3
Reaction score
0
Steve,

Don't ask me what's really wrong with the structur of you files... but as shown below, I can give you versions of your XML als XSD, that works.

Here the XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='schemaTest.xsd'> <row> <product>Product</product> </row> </root>

And following the XSD:

<?xml version ="1.0"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">

<xs:element name = "root" type="rootType"/>

<xs:complexType name="rootType">
<xs:sequence>
<xs:element name = "row" type="rowType"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="rowType">
<xs:sequence>
<xs:element name = "product" type="xs:string"/>
</xs:sequence>
</xs:complexType>

</xs:schema>

I'm not able to say, why your example doesen't work... maybe the difference is the use of "xs:" instead of "xsd:"...
And: my parser did not accept the tag "content = "elementOnly".
However: the files given here work
Bye, Frank
 

Steve Dussinger

Usenet Archive
Joined
Jul 21, 2001
Messages
9
Reaction score
0
Frank Weyand wrote:

Steve,

Don't ask me what's really wrong with the structur of you files... but as shown below, I can give you versions of your XML als XSD, that works.

Here the XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='schemaTest.xsd'> <row> ...
It turns out that it wat the declaration of the schema namespace in the xml file:

I had <root xmlns:xsi="http://www.w3c.org/2001/XMLSchema"....

it needed to be: <root
xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"...

The errors with the elementOnly tags are due to the change in the schema from the 2000/10 draft to the 2001 CR.

Thanx,
Steve
 
Status
Not open for further replies.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
548,050
Messages
3,104,249
Members
248,133
Latest member
DujonT

Latest Threads

Top