This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Error in XSD Parsing - Imported XSD not being loaded Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Error in XSD Parsing - Imported XSD not being loaded" Watch "Error in XSD Parsing - Imported XSD not being loaded" New topic
Author

Error in XSD Parsing - Imported XSD not being loaded

Sarah Jones
Greenhorn

Joined: Jul 19, 2004
Posts: 12
I am trying to validate XML with XSD. The XML has elements in two different namespaces:-

1-"http://schemas.xmlsoap.org/soap/envelope/"
2-urn:Hotel_Reserve

So in Schema I have defined elements Envelop, Header and Body in "http://schemas.xmlsoap.org/soap/envelope/" namespace and targetNamespace and imported another schema which is in "urn:Hotel_Reserve" namespace. Here it is:-

<xs:schema xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:n1="urn:Hotel_Reserve" targetNamespace="http://schemas.xmlsoap.org/soap/envelope/" elementFormDefault="qualified">
<xs:import namespace="urn:Hotel_Reserve" schemaLocation="BookingRequestBody.xsd"/>
<xs:element name="Envelope">
<xs:complexType>
<xs:sequence>
<xs:element name="Header">
<xs:complexType/>
</xs:element>
<xs:element name="Body">
<xs:complexType>
<xs:sequence>
<xs:element ref="n1:DS_HotelRes_RQ"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

I am using JDK1.5. I am able to validate the XML in a standalone Java program. I suppose here the Xerces parse is being used which is shipped with JDK itself in com.sun.org.apache.xerces* package.

The problem is when the same code is deployed in JBoss 4.0.3SP1, I get following exception:-
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'n1:DS_HotelRes_RQ' to a(n) 'element declaration' component.

It seems that when the code is deployed in JBoss, and the main XSD(which is mentioned above) is loaded and parsed, the schema in the import section is not loaded. I have checked the schema are present in the required place.

Any clue where I may be doing wrong?
Sarah Jones
Greenhorn

Joined: Jul 19, 2004
Posts: 12
Instead of parsing the schema with DOM/SAX, just loading it with URL worked with JBoss.
But I don’t know how and why?
here is the sample code:



URL schemaURL = Thread.currentThread().getContextClassLoader().getResource(WSConstants.BOOKING_SCHEMA_FILE);

SchemaFactory provBookingSchemaFactory = schemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); provBookingSchemaFactory.setErrorHandler(new InvalidXMLErrorHandler());
Schema provBookingSchema = provBookingSchemaFactory.newSchema(schemaURL);
Validator provBookingValidator = provBookingSchema.newValidator();
provBookingValidator.validate(new DOMSource(requestDoc));

Earlier I was creating the schema instance using the DOM source:-


Schema provBookingSchema = provBookingSchemaFactory.newSchema(new DOMSource(schemaDoc));
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I think it's because the schema location is a relative URL. So if you give the parser a URL, it has a base URL to apply the relative URL to. If you give the parser a DOM, it doesn't.
Sarah Jones
Greenhorn

Joined: Jul 19, 2004
Posts: 12
Yes, this makes sense.
But the question is how come the schema factory is able to load the imported schema when the schema is given as a DOM source in a standalone Java program. I faced the problem when the code was being run in JBoss server.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Presumably if the resolver doesn't have a base URL, it falls back on using the current working directory for that. If you happened to have that imported schema in the current working directory, it would work by accident.
 
Don't get me started about those stupid light bulbs.
 
subject: Error in XSD Parsing - Imported XSD not being loaded
 
Similar Threads
Validating a SOAP document using XSD
Imported XSD not being loaded- Parsing error
Unqualified elment - but still have prefix at the root
how to import 2 xsd's which have same namespace?
WSDL to service