You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Uma Sabada <Um...@Sun.COM> on 2006/06/28 21:41:38 UTC

Forcing validation against a specific schema

Hi,

Is it possible to force validation against a specific local schema 
regardless of the namespace/schemalocation in the instance document?

What I am trying to do is: If the web.xml used in an application is 
higher than the version supported by the product, I am trying to force 
validation against the highest supported version so that it doesn't 
access the web to get the schema from the instance's schemalocation.

My web.xml has the following:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


I want to validate it against a local web-app_2_4.xsd(with 
targetnamespace of http://java.sun.com/xml/ns/j2ee). This schema can be 
viewed at
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd


I have tried properties

http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation
http://apache.org/xml/properties/schema/external-schemaLocation

and turning off namespaceaware.

However, I am unable to get the desired result.

The source is:

public class MyParse extends DefaultHandler implements EntityResolver {

   MyParse() {
       try {
       FileInputStream stream = new FileInputStream("web.xml");
       URL url = new File("web.xml").toURL();
       InputSource is = new InputSource(url.toExternalForm());
       is.setByteStream(stream);
       SAXParserFactory parserFactory = SAXParserFactory.newInstance();
       parserFactory.setNamespaceAware(false);
       parserFactory.setValidating(true);
       
parserFactory.setFeature("http://apache.org/xml/features/validation/schema", 
true);
       
parserFactory.setFeature("http://xml.org/sax/features/namespace-prefixes", 
false);
       
parserFactory.setFeature("http://xml.org/sax/features/namespaces", false);
       SAXParser parser = parserFactory.newSAXParser();
       url = MyParse.class.getResource("web-app_2_4.xsd");
       XMLReader xmlReader = parser.getXMLReader();
       StringBuffer sb = new StringBuffer(200);
       sb.append("http://java.sun.com/xml/ns/javaee ");
       sb.append(url.toString());
// Setting these properties does not get the desired result
// 
xmlReader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", 
sb.toString());
//xmlReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", 
url.toString());

       xmlReader.setErrorHandler(this);
       xmlReader.setEntityResolver(this);
       xmlReader.parse(is);
       } catch (Exception e) {System.out.println("Exception e = "+e); }
   }


Thanks
-Uma



---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Forcing validation against a specific schema

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Uma.Sabada@Sun.COM wrote on 06/28/2006 10:24:52 PM:

> Michael,
> 
>
> Registering an entity resolver does not solve the problem since the 
> namespace of the schema differs from the namespace of the instance. 
> I get the following error:
> 
> Parse Error at line 14 column 20: TargetNamespace.1: Expecting namespace 
'
> http://java.sun.com/xml/
> ns/javaee', but the target namespace of the schema document is '
> http://java.sun.com/xml/ns/j2ee'.

Therein lies the problem. The namespaces need to match. You can't get 
around that. Either return a schema which matches the namespace in your 
instance doc or change the namespace in your instance so that it matches 
the target namespace of the schema you want to use.

> If I set setNamespaceAware(false), then it does not call the 
entityresolver.

Don't set that to false. Schema validation requires namespace support.

> -Uma
> 
> 
> Michael Glavassevich wrote:
> Uma.Sabada@Sun.COM wrote on 06/28/2006 03:41:38 PM:
> 
> 
> Hi,
> 
> Is it possible to force validation against a specific local schema 
> regardless of the namespace/schemalocation in the instance document?
> 
> 
> Yes. Register an EntityResolver which returns your local schema instead 
of 
> what was specified by the schemaLocation hint.
> 
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Forcing validation against a specific schema

Posted by Uma Sabada <Um...@Sun.COM>.
Michael,


Registering an entity resolver does not solve the problem since the 
namespace of the schema differs from the namespace of the instance. I 
get the following error:

Parse Error at line 14 column 20: TargetNamespace.1: Expecting namespace 
'http://java.sun.com/xml/
ns/javaee', but the target namespace of the schema document is 
'http://java.sun.com/xml/ns/j2ee'.


If I set setNamespaceAware(false), then it does not call the entityresolver.


-Uma


Michael Glavassevich wrote:

>Uma.Sabada@Sun.COM wrote on 06/28/2006 03:41:38 PM:
>
>  
>
>>Hi,
>>
>>Is it possible to force validation against a specific local schema 
>>regardless of the namespace/schemalocation in the instance document?
>>    
>>
>
>Yes. Register an EntityResolver which returns your local schema instead of 
>what was specified by the schemaLocation hint.
>
>Michael Glavassevich
>XML Parser Development
>IBM Toronto Lab
>E-mail: mrglavas@ca.ibm.com
>E-mail: mrglavas@apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
>For additional commands, e-mail: j-users-help@xerces.apache.org
>
>
>  
>

Re: Forcing validation against a specific schema

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Uma.Sabada@Sun.COM wrote on 06/28/2006 03:41:38 PM:

> Hi,
> 
> Is it possible to force validation against a specific local schema 
> regardless of the namespace/schemalocation in the instance document?

Yes. Register an EntityResolver which returns your local schema instead of 
what was specified by the schemaLocation hint.

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org