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 Mayer Crystal <mc...@pro-found.com> on 2000/08/17 17:16:10 UTC

Questions regarding XML Schema validation

I hope this is the right forum for these questions... If it is not I apologize.

I have recently switched over from DTDs to try and work with XML schema.  After
downloading and installing xerces-1.1.3 I attempted to validate an XML document
using an XML schema.  Even though I purposely entered invalid information into
the instance document I did not see any errors thrown by the parser.  To
further my confusion, I wrote a simle program and used the personal-schema.xml
and personal.xsd schema which shipped with the distribution and simply removed
the id attribure (which is defined in the schema as type='required') from
the first person element in the personal-schema.xml document, but it still
executed without complaining.  The code that I used to test this is:


import java.io.*;

import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;

import org.apache.xerces.parsers.*;


public class TestValidate {
  public static void main(String[] args) {
    DOMParser parser = new DOMParser();
    try  {
      parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
      parser.setFeature("http://xml.org/sax/features/validation", true);
      parser.setFeature("http://xml.org/sax/features/namespaces", true);
      parser.setFeature("http://apache.org/xml/features/validation/schema", true);
      parser.parse(args[0]);
    }
    catch (Exception e)  {
      e.printStackTrace();
      System.exit(1);
    }
    Document doc = parser.getDocument();
    StringWriter sw = new StringWriter();
    try  {
      OutputFormat of = new OutputFormat(doc);
      XMLSerializer ser = new XMLSerializer(sw, of);
      ser.serialize(doc);
    }
    catch (Exception e)  {
      e.printStackTrace();
    }
    System.err.println(sw.toString());

    System.exit(0);
  }
}

If I am setting/not setting some flag or if I am doing something incorrectly
or if someone could show me an example of code and xml/xsd files which create
errors when validated I would really appreciate it.

On a seperate front, I was wondering if Xerces allowed a programmer to force it
to use a specific schema.  For instance, if I had an XML document with no
namespace declaration and I wanted to use a specific schema to validate the
document against (and if possible cache the schema in memory so it would not
have to be fetched for every new XML input), is there a way to do that with the
current implementation of Xerces?

Thanks in advance,
Mayer Crystal
mcrystal@pro-found.com 



RE: Questions regarding XML Schema validation

Posted by rsanford <rs...@nolimitsystems.com>.
i asked the same question yesterday!!!

the answer i got was to use the parser method setErrorHandler.
i am getting ready to dive into the code to figure out how it
works.

have fun!

rjsjr

> -----Original Message-----
> From: Mayer Crystal [mailto:mcrystal@pro-found.com]
> Sent: Thursday, August 17, 2000 10:16 AM
> To: xerces-j-dev@xml.apache.org
> Subject: Questions regarding XML Schema validation
>
>
> I hope this is the right forum for these questions... If it is
> not I apologize.
>
> I have recently switched over from DTDs to try and work with XML
> schema.  After
> downloading and installing xerces-1.1.3 I attempted to validate
> an XML document
> using an XML schema.  Even though I purposely entered invalid
> information into
> the instance document I did not see any errors thrown by the parser.  To
> further my confusion, I wrote a simle program and used the
> personal-schema.xml
> and personal.xsd schema which shipped with the distribution and
> simply removed
> the id attribure (which is defined in the schema as type='required') from
> the first person element in the personal-schema.xml document, but it still
> executed without complaining.  The code that I used to test this is:
>
>
> import java.io.*;
>
> import org.w3c.dom.*;
> import org.apache.xerces.dom.*;
> import org.apache.xml.serialize.*;
>
> import org.apache.xerces.parsers.*;
>
>
> public class TestValidate {
>   public static void main(String[] args) {
>     DOMParser parser = new DOMParser();
>     try  {
>
> parser.setFeature("http://apache.org/xml/features/dom/defer-node-e
> xpansion", true);
>       parser.setFeature("http://xml.org/sax/features/validation", true);
>       parser.setFeature("http://xml.org/sax/features/namespaces", true);
>
> parser.setFeature("http://apache.org/xml/features/validation/schem
> a", true);
>       parser.parse(args[0]);
>     }
>     catch (Exception e)  {
>       e.printStackTrace();
>       System.exit(1);
>     }
>     Document doc = parser.getDocument();
>     StringWriter sw = new StringWriter();
>     try  {
>       OutputFormat of = new OutputFormat(doc);
>       XMLSerializer ser = new XMLSerializer(sw, of);
>       ser.serialize(doc);
>     }
>     catch (Exception e)  {
>       e.printStackTrace();
>     }
>     System.err.println(sw.toString());
>
>     System.exit(0);
>   }
> }
>
> If I am setting/not setting some flag or if I am doing something
> incorrectly
> or if someone could show me an example of code and xml/xsd files
> which create
> errors when validated I would really appreciate it.
>
> On a seperate front, I was wondering if Xerces allowed a
> programmer to force it
> to use a specific schema.  For instance, if I had an XML document with no
> namespace declaration and I wanted to use a specific schema to
> validate the
> document against (and if possible cache the schema in memory so
> it would not
> have to be fetched for every new XML input), is there a way to do
> that with the
> current implementation of Xerces?
>
> Thanks in advance,
> Mayer Crystal
> mcrystal@pro-found.com
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>