[XML] XSD Schema Validation

Discussion in 'Java' started by oziris, Sep 28, 2005.

  1. oziris

    oziris Guest

    Hi,

    Is there a way to validate a XSD schema?
    I perform a validation of a XML file according to this schema with

    <code>
    SchemaFactory schemaFactory =
    SchemaFactory.newInstance(
    XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schemaXSD = schemaFactory.newSchema(fichierXSD);
    Validator validateur = schemaXSD.newValidator();
    validateur.validate(fichierXML);
    </code>

    but I would like as well to perform a validation of *the schema*.

    Thanks.

    -o--
     
    oziris, Sep 28, 2005
    #1
    1. Advertising

  2. oziris

    Timbo Guest

    oziris wrote:
    > Hi,
    >
    > Is there a way to validate a XSD schema?
    > I perform a validation of a XML file according to this schema with
    >
    > <code>
    > SchemaFactory schemaFactory =
    > SchemaFactory.newInstance(
    > XMLConstants.W3C_XML_SCHEMA_NS_URI);
    > Schema schemaXSD = schemaFactory.newSchema(fichierXSD);
    > Validator validateur = schemaXSD.newValidator();
    > validateur.validate(fichierXML);
    > </code>
    >
    > but I would like as well to perform a validation of *the schema*.
    >

    But against what are you validating it? The XML file is being
    validated with respect to the format specified in the schema. I
    think the best you could do with regards to the XSD schema is a
    syntax check, and a few other checks the jaxb gives you, such as
    no duplicate names etc. Anything other validation would need to be
    by hand.
     
    Timbo, Sep 28, 2005
    #2
    1. Advertising

  3. oziris

    Chris Guest

    Re: XSD Schema Validation

    So you want a schema for a schema? You can just treat the schema as an
    XML file and validate it as normal. If you are looking to make sure
    it's a valid schema file there are standard xsd's and dtd's out there
    that will validate schemas.

    If that's not what you are looking to do, I guess you'll have to
    elaborate on your question.

    Thanks,

    ~Chris
     
    Chris, Sep 28, 2005
    #3
  4. oziris wrote:


    > Is there a way to validate a XSD schema?
    > I perform a validation of a XML file according to this schema with
    >
    > <code>
    > SchemaFactory schemaFactory =
    > SchemaFactory.newInstance(
    > XMLConstants.W3C_XML_SCHEMA_NS_URI);
    > Schema schemaXSD = schemaFactory.newSchema(fichierXSD);


    I think that call compiles and validates the schema, you need to set up
    an error handler to be informed about any errors during compilation and
    validation of the schema e.g. do
    schemaFactory.setErrorHandler(yourErrorHandler);
    before you call newSchema. The error handler needs to implement
    org.xml.sax.ErrorHandler.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 28, 2005
    #4
  5. oziris

    oziris Guest

    Re: XSD Schema Validation

    Hi Chris,

    Thanks for your reply.
    Where could I get such standard xsd's ? I'm a little bit lost :-(

    -o--
     
    oziris, Sep 28, 2005
    #5
  6. oziris

    oziris Guest

    Re: XSD Schema Validation

    Hi Martin,

    I thought a SAXParseException was thrown even if no ErrorHandler were
    defined.
    That is not the case in my app.

    -o--
     
    oziris, Sep 28, 2005
    #6
  7. oziris

    oziris Guest

    Re: XSD Schema Validation

    Hi Timbo,

    It's what I thought at first but I read begins of pseudo-solution. I
    find a kind of confirmation that it's possible or not.

    -o--
     
    oziris, Sep 28, 2005
    #7
  8. oziris

    oziris Guest

    Re: XSD Schema Validation

    Please read "I look for" instead of "I find". Sorry for my poor english
    :-(

    -o--
     
    oziris, Sep 28, 2005
    #8
  9. oziris

    Chris Guest

    Re: XSD Schema Validation

    http://www.w3.org/2001/XMLSchema.xsd is the reference schema for the
    schema language.

    Beware it's big, and you really don't want to validate your schema
    everytime you need to validate an XML file. But when you are authoring
    your schema, it's useful, especially when you don't have a schema
    authoring tool

    ~Chris
     
    Chris, Sep 28, 2005
    #9
  10. Re: XSD Schema Validation

    oziris wrote:


    > I thought a SAXParseException was thrown even if no ErrorHandler were
    > defined.


    I have just tried it here to set up a simple Error handler which reports
    any errors/warnings to System.out and then I have tried it with a schema
    that intentionally contains some errors (e.g. typos like minOcurs for
    the minOccurs attribute) and those errors are reported to the error
    handlers as follows:

    Recoverable parse error: org.xml.sax.SAXParseException:
    s4s-att-not-allowed: Attribute 'minOcurs' cannot appear in element
    'element'.

    where the error handler is set as follows:

    schemaFactory.setErrorHandler(new ErrorHandler () {
    public void error (SAXParseException parseException) {
    System.out.println("Recoverable parse error: " + parseException);
    }
    public void fatalError (SAXParseException parseException) {
    System.out.println("Fatal parse error: " + parseException);
    }
    public void warning (SAXParseException parseException) {
    System.out.println("parse warning: " + parseException);
    }
    });

    So that way you get any errors reported during compilation and
    validation of the schema.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 28, 2005
    #10
  11. oziris

    Ian Pilcher Guest

    Re: XSD Schema Validation

    Chris wrote:
    > http://www.w3.org/2001/XMLSchema.xsd is the reference schema for the
    > schema language.


    Note that many parsers, including the one in Sun's JDKs can't handle
    validating schemas. In general the best practical way to validate a
    schema is to use it to validate an XML file.

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Sep 28, 2005
    #11
  12. oziris

    Chris Guest

    Re: XSD Schema Validation

    Why wouldn't any mature validating parser not be able to validate a
    schema against the one I provided?

    A schema (xsd) is just xml in a certain namespace just like any other
    xml document. What am I missing?

    Thanks,

    ~Chris
     
    Chris, Sep 28, 2005
    #12
  13. oziris

    Ian Pilcher Guest

    Re: XSD Schema Validation

    Chris wrote:
    > Why wouldn't any mature validating parser not be able to validate a
    > schema against the one I provided?


    Bugs?

    > A schema (xsd) is just xml in a certain namespace just like any other
    > xml document. What am I missing?


    Try it.

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Sep 28, 2005
    #13
  14. oziris

    Chris Guest

    Re: XSD Schema Validation

    Once I stripped the DTD declaration (the <!DOCTYPE> section) at the
    top, I had no problem validating schemas against the aformentioned
    schema with org.apache.xerces.parsers.SAXParser. Sun uses xerces
    AFAIK.

    ~Chris
     
    Chris, Sep 28, 2005
    #14
  15. oziris

    Roedy Green Guest

    On 28 Sep 2005 07:04:17 -0700, "oziris" <>
    wrote or quoted :

    >Is there a way to validate a XSD schema?


    see http://mindprod.com/jgloss/xsd.html

    There are a number of external tools. I think way you do it inside
    Java is to use the Xerces part of JAXP. I have not used the tool so I
    can't be more specific. When you figure it out, please post a code
    snippet I can include under that glossary entry.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 28, 2005
    #15
  16. oziris

    Roedy Green Guest

    On Wed, 28 Sep 2005 16:39:06 +0200, Martin Honnen <>
    wrote or quoted :

    >I think that call compiles and validates the schema,


    Does the schema itself get parsed, and validated every time you
    validate an XML document? If so, this seems a hefty penalty to pay. I
    would have expected the parsed schema to be cached somehow.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 28, 2005
    #16
  17. Roedy Green wrote:

    > On Wed, 28 Sep 2005 16:39:06 +0200, Martin Honnen <>
    > wrote or quoted :
    >
    >
    >>I think that call compiles and validates the schema,

    >
    >
    > Does the schema itself get parsed, and validated every time you
    > validate an XML document?


    No, when you look in my answer you can see that "that call" refers to
    Schema schemaXSD = schemaFactory.newSchema(fichierXSD);
    so there the schema file is parsed and compiled into a
    javax.xml.validation.Schema object instance.

    That instance can then be used multiple times to create a validator.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 29, 2005
    #17
  18. oziris

    Roedy Green Guest

    On Wed, 28 Sep 2005 16:39:06 +0200, Martin Honnen <>
    wrote or quoted :

    >> SchemaFactory schemaFactory =
    >> SchemaFactory.newInstance(
    >> XMLConstants.W3C_XML_SCHEMA_NS_URI);
    >> Schema schemaXSD = schemaFactory.newSchema(fichierXSD);

    >
    >I think that call compiles and validates the schema, you need to set up
    >an error handler to be informed about any errors during compilation and
    >validation of the schema e.g. do
    > schemaFactory.setErrorHandler(yourErrorHandler);
    >before you call newSchema. The error handler needs to implement
    >org.xml.sax.ErrorHandler.


    I was enshrining a heavily commented variant of your code as an
    example at http://mindprod.com/jgloss/xsd.html

    I wondered why you had both Schemas and Validators. I would have
    thought validate would be a method of Schema. When does the Schema
    itself get parsed/validated. When you create the Schema or the
    Validator?

    Schema schemaXSD = schemaFactory.newSchema( new File ( "myschema.xsd"
    ) );

    // Create a Validator capable of validating XML files according to my
    custom schema.
    Validator validator = schemaXSD.newValidator();

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 29, 2005
    #18
  19. oziris

    Roedy Green Guest

    On Thu, 29 Sep 2005 14:09:54 GMT, Roedy Green
    <> wrote or quoted :

    >I wondered why you had both Schemas and Validators.


    I think the answer is the Schemas have more functions in life than
    acting as Validators. Therefore various functionality was split off
    into separate classes.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 29, 2005
    #19
  20. oziris

    oziris Guest

    Re: XSD Schema Validation

    Hi all,

    To end this thread I confirm the Martin solution which consists in
    setting an ErrorHandler to the SchemaFactory.
    That reports correctly all errors relating to the XSD schema grammar.

    Merci beaucoup à tout le monde.

    -o--
     
    oziris, Sep 30, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,541
    Markus
    Nov 23, 2005
  2. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,264
    Stanimir Stamenkov
    Apr 25, 2005
  3. stiank81
    Replies:
    4
    Views:
    15,403
    stiank81
    Jun 26, 2005
  4. Rushi
    Replies:
    1
    Views:
    552
  5. Replies:
    5
    Views:
    1,027
    Brian McCauley
    Nov 29, 2006
Loading...

Share This Page