You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Hiranya Jayathilaka <hi...@gmail.com> on 2009/06/11 11:06:57 UTC

XML Schema Validation with Xerces2

Hi Folks,

I would like to know how I can validate an XML schema (1.0) document using
Xerces2. I want  to validate  a given XML schema document against the schema
for schemas programmatically. Can somebody please provide me the
instructions on the right way to do this in Java? A pointer to a sample code
fragment is most appreciated.

Also can somebody tell me whether validating a schema causes any imported
schema documents to be validated?

Thanks,
Hiranya

-- 
Hiranya Jayathilaka
Software Engineer;
WSO2 Inc.;  http://wso2.org
E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
Blog: http://techfeast-hiranya.blogspot.com

Re: XML Schema Validation with Xerces2

Posted by Hiranya Jayathilaka <hi...@gmail.com>.
On Fri, Jun 12, 2009 at 5:39 PM, Mukul Gandhi <ga...@gmail.com>wrote:

> Hi Michael,
>   Thanks for letting us know more about these APIs. The insights are
> quite helpful..


+1

Thanks for the tips Michael.

Thanks,
Hiranya


>
>
> On Fri, Jun 12, 2009 at 5:30 PM, Michael
> Glavassevich<mr...@ca.ibm.com> wrote:
> > To add to that, if it was a user asking I'd recommend SchemaFactory of
> those
> > three choices since it's in Java 5+ and would work in environments where
> > Xerces isn't available.
> >
> > Michael Glavassevich
> > XML Parser Development
> > IBM Toronto Lab
> > E-mail: mrglavas@ca.ibm.com
> > E-mail: mrglavas@apache.org
>
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org
>
>


-- 
Hiranya Jayathilaka
Software Engineer;
WSO2 Inc.;  http://wso2.org
E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
Blog: http://techfeast-hiranya.blogspot.com

Re: XML Schema Validation with Xerces2

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
   Thanks for letting us know more about these APIs. The insights are
quite helpful..

On Fri, Jun 12, 2009 at 5:30 PM, Michael
Glavassevich<mr...@ca.ibm.com> wrote:
> To add to that, if it was a user asking I'd recommend SchemaFactory of those
> three choices since it's in Java 5+ and would work in environments where
> Xerces isn't available.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org



-- 
Regards,
Mukul Gandhi

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


Re: XML Schema Validation with Xerces2

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
To add to that, if it was a user asking I'd recommend SchemaFactory of
those three choices since it's in Java 5+ and would work in environments
where Xerces isn't available.

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

mrglavas@ca.ibm.com wrote on 06/12/2009 07:52:11 AM:

> Hi Mukul,
>
> All of those approaches end up going through similar codepaths (i.e.
> XMLSchemaLoader) and are appropriate for a validating a schema,
> though those APIs do exist primarily for other purposes:
>
> SchemaFactory - entry point into the JAXP Validation API for loading
> schemas for validation
> XSLoader - entry point into the XML Schema API for obtaining an
> XSModel for analysis/processing of the component model
> XMLGrammarPreparser - an API for preparsing schemas and DTDs for use
> in grammar caching (i.e. a lower-level alternative to SchemaFactory)
>
> Thanks.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org
>
> Mukul Gandhi <ga...@gmail.com> wrote on 06/12/2009 06:14:47 AM:
>
> > Hi Hiranya,
> >    Interestingly, I have found an easy way to validate a Schema
> > document. We just need to use the JAXP validation API (which will be
> > visible to the user normally).
> >
> > Below is a code snippet for this:
> >
> > SchemaFactory sf = SchemaFactory.newInstance ..
> > sf.setErrorHandler ..
> > Schema s = sf.newSchema(new StreamSource(schemapath));
> >
> > The 'newSchema' call would not succeed if XML Schema has an error.
> >
> > The XSModel or the Grammar technique you wrote, is a low level API
> > which is normally used within Xerces.
> >
> > On Fri, Jun 12, 2009 at 11:49 AM, Mukul Gandhi<gandhi.mukul@gmail.
> com> wrote:
> > >> By going through the Xerces samples I came up with the following
code to
> > >> validate an XML schema document.
> > >>
> > >>         XMLGrammarPreparser preparser = new XMLGrammarPreparser();
> > >>         preparser.registerPreparser(XMLGrammarDescription.
> > XML_SCHEMA, null);
> > >>         preparser.setFeature
("http://xml.org/sax/features/namespaces",
> > >> true);
> > >>         preparser.setFeature
("http://xml.org/sax/features/validation",
> > >> true);
> > >>
> > >> preparser.setFeature
("http://apache.org/xml/features/validation/schema",
> > >> true);
> > >>         preparser.setErrorHandler(new MyErrorHandler());
> > >>         Grammar g =
> > >> preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
> > >>                                  new XMLInputSource(null, xsdUrl,
null));
> > >>
> > >> This seems to be working really well. It even validates any
> > imported schemas
> > >> etc. All the errors are reported to the registered error
> handler.WDYT about
> > >> this approach over the XSModel construction approach?
> > >
> > > I think, you can use either of the approaches. The XSModel approach I
> > > mentioned also validates imported schemas as well.
> > >
> > > Infact, the 'loadURI' method I mentioned, builds XSModel from the
> > > Grammar object (it does something like, ((XSGrammar)
> > > fSchemaLoader.loadGrammar(new XMLInputSource(null, uri,
> > > null))).toXSModel().
> >
> >
> > --
> > Regards,
> > Mukul Gandhi
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> > For additional commands, e-mail: j-dev-help@xerces.apache.org

Re: XML Schema Validation with Xerces2

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

All of those approaches end up going through similar codepaths (i.e.
XMLSchemaLoader) and are appropriate for a validating a schema, though
those APIs do exist primarily for other purposes:

SchemaFactory - entry point into the JAXP Validation API for loading
schemas for validation
XSLoader - entry point into the XML Schema API for obtaining an XSModel for
analysis/processing of the component model
XMLGrammarPreparser - an API for preparsing schemas and DTDs for use in
grammar caching (i.e. a lower-level alternative to SchemaFactory)

Thanks.

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

Mukul Gandhi <ga...@gmail.com> wrote on 06/12/2009 06:14:47 AM:

> Hi Hiranya,
>    Interestingly, I have found an easy way to validate a Schema
> document. We just need to use the JAXP validation API (which will be
> visible to the user normally).
>
> Below is a code snippet for this:
>
> SchemaFactory sf = SchemaFactory.newInstance ..
> sf.setErrorHandler ..
> Schema s = sf.newSchema(new StreamSource(schemapath));
>
> The 'newSchema' call would not succeed if XML Schema has an error.
>
> The XSModel or the Grammar technique you wrote, is a low level API
> which is normally used within Xerces.
>
> On Fri, Jun 12, 2009 at 11:49 AM, Mukul Gandhi<ga...@gmail.com>
wrote:
> >> By going through the Xerces samples I came up with the following code
to
> >> validate an XML schema document.
> >>
> >>         XMLGrammarPreparser preparser = new XMLGrammarPreparser();
> >>         preparser.registerPreparser(XMLGrammarDescription.
> XML_SCHEMA, null);
> >>         preparser.setFeature("http://xml.org/sax/features/namespaces",
> >> true);
> >>         preparser.setFeature("http://xml.org/sax/features/validation",
> >> true);
> >>
> >> preparser.setFeature
("http://apache.org/xml/features/validation/schema",
> >> true);
> >>         preparser.setErrorHandler(new MyErrorHandler());
> >>         Grammar g =
> >> preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
> >>                                  new XMLInputSource(null, xsdUrl,
null));
> >>
> >> This seems to be working really well. It even validates any
> imported schemas
> >> etc. All the errors are reported to the registered error handler.WDYT
about
> >> this approach over the XSModel construction approach?
> >
> > I think, you can use either of the approaches. The XSModel approach I
> > mentioned also validates imported schemas as well.
> >
> > Infact, the 'loadURI' method I mentioned, builds XSModel from the
> > Grammar object (it does something like, ((XSGrammar)
> > fSchemaLoader.loadGrammar(new XMLInputSource(null, uri,
> > null))).toXSModel().
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org

Re: XML Schema Validation with Xerces2

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Hiranya,
   Interestingly, I have found an easy way to validate a Schema
document. We just need to use the JAXP validation API (which will be
visible to the user normally).

Below is a code snippet for this:

SchemaFactory sf = SchemaFactory.newInstance ..
sf.setErrorHandler ..
Schema s = sf.newSchema(new StreamSource(schemapath));

The 'newSchema' call would not succeed if XML Schema has an error.

The XSModel or the Grammar technique you wrote, is a low level API
which is normally used within Xerces.

On Fri, Jun 12, 2009 at 11:49 AM, Mukul Gandhi<ga...@gmail.com> wrote:
>> By going through the Xerces samples I came up with the following code to
>> validate an XML schema document.
>>
>>         XMLGrammarPreparser preparser = new XMLGrammarPreparser();
>>         preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
>>         preparser.setFeature("http://xml.org/sax/features/namespaces",
>> true);
>>         preparser.setFeature("http://xml.org/sax/features/validation",
>> true);
>>
>> preparser.setFeature("http://apache.org/xml/features/validation/schema",
>> true);
>>         preparser.setErrorHandler(new MyErrorHandler());
>>         Grammar g =
>> preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
>>                                  new XMLInputSource(null, xsdUrl, null));
>>
>> This seems to be working really well. It even validates any imported schemas
>> etc. All the errors are reported to the registered error handler. WDYT about
>> this approach over the XSModel construction approach?
>
> I think, you can use either of the approaches. The XSModel approach I
> mentioned also validates imported schemas as well.
>
> Infact, the 'loadURI' method I mentioned, builds XSModel from the
> Grammar object (it does something like, ((XSGrammar)
> fSchemaLoader.loadGrammar(new XMLInputSource(null, uri,
> null))).toXSModel().


-- 
Regards,
Mukul Gandhi

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


Re: XML Schema Validation with Xerces2

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Hiranya,

On Fri, Jun 12, 2009 at 10:45 AM, Hiranya
Jayathilaka<hi...@gmail.com> wrote:
> By going through the Xerces samples I came up with the following code to
> validate an XML schema document.
>
>         XMLGrammarPreparser preparser = new XMLGrammarPreparser();
>         preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
>         preparser.setFeature("http://xml.org/sax/features/namespaces",
> true);
>         preparser.setFeature("http://xml.org/sax/features/validation",
> true);
>
> preparser.setFeature("http://apache.org/xml/features/validation/schema",
> true);
>         preparser.setErrorHandler(new MyErrorHandler());
>         Grammar g =
> preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
>                                  new XMLInputSource(null, xsdUrl, null));
>
> This seems to be working really well. It even validates any imported schemas
> etc. All the errors are reported to the registered error handler. WDYT about
> this approach over the XSModel construction approach?

I think, you can use either of the approaches. The XSModel approach I
mentioned also validates imported schemas as well.

Infact, the 'loadURI' method I mentioned, builds XSModel from the
Grammar object (it does something like, ((XSGrammar)
fSchemaLoader.loadGrammar(new XMLInputSource(null, uri,
null))).toXSModel().


-- 
Regards,
Mukul Gandhi

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


Re: XML Schema Validation with Xerces2

Posted by Hiranya Jayathilaka <hi...@gmail.com>.
Hi Folks,

By going through the Xerces samples I came up with the following code to
validate an XML schema document.

        XMLGrammarPreparser preparser = new XMLGrammarPreparser();
        preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
        preparser.setFeature("http://xml.org/sax/features/namespaces",
true);
        preparser.setFeature("http://xml.org/sax/features/validation",
true);
        preparser.setFeature("
http://apache.org/xml/features/validation/schema", true);
        preparser.setErrorHandler(new MyErrorHandler());
        Grammar g =
preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,
                                 new XMLInputSource(null, xsdUrl, null));

This seems to be working really well. It even validates any imported schemas
etc. All the errors are reported to the registered error handler. WDYT about
this approach over the XSModel construction approach?

Thanks,
Hiranya

On Thu, Jun 11, 2009 at 6:47 PM, Mukul Gandhi <ga...@gmail.com>wrote:

> Hi Michael,
>   I saw the code in XSLoaderImpl.java where it's written like (in the
> 'loadURI' method):
>
> catch (Exception e){
>   fSchemaLoader.reportDOMFatalError(e);
>   return null;
> }
>
> The definition of reportDOMFatalError is something like below:
>
> void reportDOMFatalError(Exception e) {
>  if (fErrorHandler != null) {
>    DOMErrorImpl error = new DOMErrorImpl();
>    error.fException = e;
>    error.fMessage = e.getMessage();
>    error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
>    fErrorHandler.getErrorHandler().handleError(error);
>  }
> }
>
> Looking at the method reportDOMFatalError, I thought some default
> error handler has been registered with the XMLSchemaLoader.
>
> But your suggestion looks great, to register a custom error handler. I
> would explore it further and would get back with questions to the
> list..
>
> On Thu, Jun 11, 2009 at 6:25 PM, Michael
> Glavassevich<mr...@ca.ibm.com> wrote:
> > I didn't see that in the snippet you posted. If you want programatic
> access
> > to error information then you need to explicitly register an error
> handler:
> >
> > DOMErrorHandler myErrorHandler = ...;
> >
> > XSImplementation xsImpl = (XSImplementation)
> > registry.getDOMImplementation("XS-Loader");
> > XSLoader xsLoader = xsImpl.createXSLoader(null);
> >
> > DOMConfiguration config = xsLoader.getConfig();
> > config.setParameter("error-handler", myErrorHandler); // <-- set the
> error
> > handler
>
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org
>
>


-- 
Hiranya Jayathilaka
Software Engineer;
WSO2 Inc.;  http://wso2.org
E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
Blog: http://techfeast-hiranya.blogspot.com

Re: XML Schema Validation with Xerces2

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,
   I saw the code in XSLoaderImpl.java where it's written like (in the
'loadURI' method):

catch (Exception e){
   fSchemaLoader.reportDOMFatalError(e);
   return null;
}

The definition of reportDOMFatalError is something like below:

void reportDOMFatalError(Exception e) {
  if (fErrorHandler != null) {
    DOMErrorImpl error = new DOMErrorImpl();
    error.fException = e;
    error.fMessage = e.getMessage();
    error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
    fErrorHandler.getErrorHandler().handleError(error);
  }
}

Looking at the method reportDOMFatalError, I thought some default
error handler has been registered with the XMLSchemaLoader.

But your suggestion looks great, to register a custom error handler. I
would explore it further and would get back with questions to the
list..

On Thu, Jun 11, 2009 at 6:25 PM, Michael
Glavassevich<mr...@ca.ibm.com> wrote:
> I didn't see that in the snippet you posted. If you want programatic access
> to error information then you need to explicitly register an error handler:
>
> DOMErrorHandler myErrorHandler = ...;
>
> XSImplementation xsImpl = (XSImplementation)
> registry.getDOMImplementation("XS-Loader");
> XSLoader xsLoader = xsImpl.createXSLoader(null);
>
> DOMConfiguration config = xsLoader.getConfig();
> config.setParameter("error-handler", myErrorHandler); // <-- set the error
> handler



-- 
Regards,
Mukul Gandhi

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


Re: XML Schema Validation with Xerces2

Posted by Hiranya Jayathilaka <hi...@gmail.com>.
Hi Michael,

On Thu, Jun 11, 2009 at 6:25 PM, Michael Glavassevich
<mr...@ca.ibm.com>wrote:

> Hi Mukul,
>
> Mukul Gandhi <ga...@gmail.com> wrote on 06/11/2009 07:45:20 AM:
>
> > Hi Hiranya,
> >
> > On Thu, Jun 11, 2009 at 4:05 PM, Hiranya
> > Jayathilaka<hi...@gmail.com> wrote:
> > > This approach seems to be working. It logs error messages when the XSD
> is
> > > invalid. But how do I know if something went wrong at Java code level?
> >
> > I do not understand the question, "if something went wrong at Java
> > code level?". There could be something wrong with the Schema, which
> > this Java program reports.
> > Could you please clarify what you mean.
> >
> > > Is there any return value or exception that I could capture in my code?
> >
> > I think, the code I posted utilizes the error handler configured with
> > the Schema loader, to report the errors.
>
> I didn't see that in the snippet you posted. If you want programatic access
> to error information then you need to explicitly register an error handler:
>
> DOMErrorHandler myErrorHandler = ...;
>
> XSImplementation xsImpl = (XSImplementation) registry.getDOMImplementation(
> "XS-Loader");
> XSLoader xsLoader = xsImpl.createXSLoader(null);
>
> DOMConfiguration config = xsLoader.getConfig();
> config.setParameter("error-handler", myErrorHandler); // <-- set the error
> handler
>

Thanks for the code snippet. This is what I was looking for - A way to
detect schema errors  programmatically  (in this case by registering a
custom error handler).

Thanks,
Hiranya

>
>
> XSModel xsModel = xsLoader.loadURI(xsdUri);
>
> > --
> > Regards,
> > Mukul Gandhi
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> > For additional commands, e-mail: j-dev-help@xerces.apache.org
>
> Thanks.
>
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org
>



-- 
Hiranya Jayathilaka
Software Engineer;
WSO2 Inc.;  http://wso2.org
E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
Blog: http://techfeast-hiranya.blogspot.com

Re: XML Schema Validation with Xerces2

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

Mukul Gandhi <ga...@gmail.com> wrote on 06/11/2009 07:45:20 AM:

> Hi Hiranya,
>
> On Thu, Jun 11, 2009 at 4:05 PM, Hiranya
> Jayathilaka<hi...@gmail.com> wrote:
> > This approach seems to be working. It logs error messages when the XSD
is
> > invalid. But how do I know if something went wrong at Java code level?
>
> I do not understand the question, "if something went wrong at Java
> code level?". There could be something wrong with the Schema, which
> this Java program reports.
> Could you please clarify what you mean.
>
> > Is there any return value or exception that I could capture in my code?
>
> I think, the code I posted utilizes the error handler configured with
> the Schema loader, to report the errors.

I didn't see that in the snippet you posted. If you want programatic access
to error information then you need to explicitly register an error handler:

DOMErrorHandler myErrorHandler = ...;

XSImplementation xsImpl = (XSImplementation) registry.getDOMImplementation(
"XS-Loader");
XSLoader xsLoader = xsImpl.createXSLoader(null);

DOMConfiguration config = xsLoader.getConfig();
config.setParameter("error-handler", myErrorHandler); // <-- set the error
handler

XSModel xsModel = xsLoader.loadURI(xsdUri);

> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org

Thanks.>

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

Re: XML Schema Validation with Xerces2

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Hiranya,

On Thu, Jun 11, 2009 at 4:05 PM, Hiranya
Jayathilaka<hi...@gmail.com> wrote:
> This approach seems to be working. It logs error messages when the XSD is
> invalid. But how do I know if something went wrong at Java code level?

I do not understand the question, "if something went wrong at Java
code level?". There could be something wrong with the Schema, which
this Java program reports.
Could you please clarify what you mean.

> Is there any return value or exception that I could capture in my code?

I think, the code I posted utilizes the error handler configured with
the Schema loader, to report the errors.


-- 
Regards,
Mukul Gandhi

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


Re: XML Schema Validation with Xerces2

Posted by Hiranya Jayathilaka <hi...@gmail.com>.
Hi Mukul,

On Thu, Jun 11, 2009 at 3:35 PM, Mukul Gandhi <ga...@gmail.com>wrote:

> Hi Hiranya,
>  I think, an easy way to do this is to try building an XSModel
> pragmatically.
>
> Here is a sample code fragment which does this:
>
> XSLoaderImpl xsLoader = new XSLoaderImpl();
> XSModel xsModel = xsLoader.loadURI(xsdUri);


This approach seems to be working. It logs error messages when the XSD is
invalid. But how do I know if something went wrong at Java code level? Is
there any return value or exception that I could capture in my code?

Thanks,
Hiranya


>
> We can't build an XSModel, from an invalid XML Schema document. The
> call, loadURI would give an error in case of invalid Schema documents.
>
> On Thu, Jun 11, 2009 at 2:36 PM, Hiranya
> Jayathilaka<hi...@gmail.com> wrote:
> > Hi Folks,
> >
> > I would like to know how I can validate an XML schema (1.0) document
> using
> > Xerces2. I want  to validate  a given XML schema document against the
> schema
> > for schemas programmatically. Can somebody please provide me the
> > instructions on the right way to do this in Java? A pointer to a sample
> code
> > fragment is most appreciated.
> >
> > Also can somebody tell me whether validating a schema causes any imported
> > schema documents to be validated?
> >
> > Thanks,
> > Hiranya
> >
> > --
> > Hiranya Jayathilaka
> > Software Engineer;
> > WSO2 Inc.;  http://wso2.org
> > E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
> > Blog: http://techfeast-hiranya.blogspot.com
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org
>
>


-- 
Hiranya Jayathilaka
Software Engineer;
WSO2 Inc.;  http://wso2.org
E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
Blog: http://techfeast-hiranya.blogspot.com

Re: XML Schema Validation with Xerces2

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Michael,

On Thu, Jun 11, 2009 at 6:09 PM, Michael
Glavassevich<mr...@ca.ibm.com> wrote:
> Actually you can build an XSModel from invalid XML Schema documents. Upon
> encountering an error Xerces will try to recover and repair the schema
> document. (Sandy did some work [1] recently to improve the error recovery.)
> Each of these errors will be reported to an error handler if you've
> registered one and that error handler can choose to abort if the application
> doesn't want to accept an XSModel built from an invalid schema.

Thanks for sharing this information.

This is interesting work. I'll look at Sandy's work to learn more
about this facility.



-- 
Regards,
Mukul Gandhi

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


Re: XML Schema Validation with Xerces2

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Hi Mukul,

Mukul Gandhi <ga...@gmail.com> wrote on 06/11/2009 06:05:46 AM:

> Hi Hiranya,
>   I think, an easy way to do this is to try building an XSModel
pragmatically.
>
> Here is a sample code fragment which does this:
>
> XSLoaderImpl xsLoader = new XSLoaderImpl();
> XSModel xsModel = xsLoader.loadURI(xsdUri);
>
> We can't build an XSModel, from an invalid XML Schema document. The
> call, loadURI would give an error in case of invalid Schema documents.

Actually you can build an XSModel from invalid XML Schema documents. Upon
encountering an error Xerces will try to recover and repair the schema
document. (Sandy did some work [1] recently to improve the error recovery.)
Each of these errors will be reported to an error handler if you've
registered one and that error handler can choose to abort if the
application doesn't want to accept an XSModel built from an invalid schema.

> On Thu, Jun 11, 2009 at 2:36 PM, Hiranya
> Jayathilaka<hi...@gmail.com> wrote:
> > Hi Folks,
> >
> > I would like to know how I can validate an XML schema (1.0) document
using
> > Xerces2. I want  to validate  a given XML schema document against the
schema
> > for schemas programmatically. Can somebody please provide me the
> > instructions on the right way to do this in Java? A pointer to a sample
code
> > fragment is most appreciated.
> >
> > Also can somebody tell me whether validating a schema causes any
imported
> > schema documents to be validated?
> >
> > Thanks,
> > Hiranya
> >
> > --
> > Hiranya Jayathilaka
> > Software Engineer;
> > WSO2 Inc.;  http://wso2.org
> > E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
> > Blog: http://techfeast-hiranya.blogspot.com
>
>
> --
> Regards,
> Mukul Gandhi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-dev-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-dev-help@xerces.apache.org

Thanks.

[1] http://issues.apache.org/jira/browse/XERCESJ-1372

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

Re: XML Schema Validation with Xerces2

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Hiranya,
  I think, an easy way to do this is to try building an XSModel pragmatically.

Here is a sample code fragment which does this:

XSLoaderImpl xsLoader = new XSLoaderImpl();
XSModel xsModel = xsLoader.loadURI(xsdUri);

We can't build an XSModel, from an invalid XML Schema document. The
call, loadURI would give an error in case of invalid Schema documents.

On Thu, Jun 11, 2009 at 2:36 PM, Hiranya
Jayathilaka<hi...@gmail.com> wrote:
> Hi Folks,
>
> I would like to know how I can validate an XML schema (1.0) document using
> Xerces2. I want  to validate  a given XML schema document against the schema
> for schemas programmatically. Can somebody please provide me the
> instructions on the right way to do this in Java? A pointer to a sample code
> fragment is most appreciated.
>
> Also can somebody tell me whether validating a schema causes any imported
> schema documents to be validated?
>
> Thanks,
> Hiranya
>
> --
> Hiranya Jayathilaka
> Software Engineer;
> WSO2 Inc.;  http://wso2.org
> E-mail: hiranya@wso2.com;  Mobile: +94 77 633 3491
> Blog: http://techfeast-hiranya.blogspot.com


-- 
Regards,
Mukul Gandhi

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