File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in XSD Parsing - Imported XSD not being loaded

 
Sarah Jones
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic