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 Harun Altay <ha...@grids.ucs.indiana.edu> on 2002/05/14 21:53:36 UTC

An interesting XML Schema validation error.

Hello,

I have a problem with xml document validation.

Here is a source code, the xml document to be validated and the error
message. This source code is running without any error with the xerces of
JBuilder6. But failing to run on the standard xerces jar files from
www.apache.org.

Fact-1:

xerces coming with JBuilder6 : 1.767 bytes

xerces 1.4.3 from Apache : 769 bytes
xerces 2.0.0. from Apache : 769 bytes
xerces 2.0.1 from Apache : 769 bytes

conclusion: JBuilder's xerces includes something extra.

Fact-2:

I have 3 different examples, 1 from www.wrox.com, for the book: XML Schema,
2 examples from http://java.sun.com.  All of them are running on JBuilder's
xerces, but all of them giving error in apache's xerces: 1.4.3, 2.0.0,
2.1.0.

Question:

How can I run the same xml schema validation programs on the standard xerces
from apache?



please look at the example below:
---------------------------------------------------------

1. CODE
2. ERROR
3. XML DOCUMENT


1. CODE
----------------------------------------------------------
        parser.setFeature( "http://xml.org/sax/features/validation", true);

    //    "instance" is the String variable for the address of the xml
document.

    //    parser.parse(instance); --> THIS MUST BE A BUG! This line is
causing problem!

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

NOT: Please find complete source code at the end.



2. ERROR
-----------------------------------------------------------

[Error] Element type "okc" must be declared. line 11 column
2 -C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "xsi:schemaLocation" must be declared for element type
"okc". line 11 column 2 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "version" must be declared for element type "okc". line 11
column 2 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "event" must be declared. line 13 column 8 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 14 column 56 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
14 column 56 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
14 column 56 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 15 column 71 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
15 column 71 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
15 column 71 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 16 column 57 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
16 column 57 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
16 column 57 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 17 column 50 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
17 column 50 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found

[Error] Attribute "uri" must be declared for element type "definition". line
17 column 50 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "comment" must be declared. line 18 column 11 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "sender" must be declared. line 19 column 10 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "distribution" must be declared. line 20 column 16 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "organization" must be declared. line 21 column 16 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "update" must be declared. line 23 column 36 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "createuri" must be declared for element type "update".
line 23 column 36 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "keywords" must be declared. line 25 column 12 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "message" must be declared. line 26 column 33 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "whitespace" must be declared for element type "message".
line 26 column 33 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "keyword" must be declared. line 27 column 40 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "filingdate" must be declared. line 29 column 14 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 30 column 31 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 30 column 31 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found

[Error] Element type "cg:category" must be declared. line 31 column 42 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 31 column 42 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 31 column 42 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 32 column 43 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 32 column 43 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 32 column 43 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found

[Error] Element type "cg:category" must be declared. line 33 column 48 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 33 column 48 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "other" must be declared for element type "cg:category".
line 33 column 48 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 34 column 75 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "other" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found

Complete 0 warnings 44 errors 0 fatal errors
[Error] Element type "cg:messageType" must be declared. line 35 column 18 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:messageType" must be declared. line 36 column 18 -
C:\jprojects\swing\swing2\template2-v2.xml




3. XML DOCUMENT
-----------------------------------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<okc
 xmlns="http://grids.ucs.indiana.edu/okc/schema/admin/ver/2"
 xmlns:cg="http://grids.ucs.indiana.edu/okc/schema/cg/ver/1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=
  "http://grids.ucs.indiana.edu/okc/schema/admin/ver/2
  http://grids.ucs.indiana.edu/schemas/okc-v2.xsd
  http://grids.ucs.indiana.edu/okc/schema/cg/ver/1
  http://grids.ucs.indiana.edu/schemas/commgrids-v1.xsd" version="2"
>

<event>
 <definition ref="anydefinition" uri="gxos://someuri"/>
 <definition ref="anotherdefinition" uri="gxos://someuri/anotheruri"/>
 <definition ref="me" uri="gxos://someuri/users/$user"/>
 <definition ref="ideas" uri="$me/ideas/$today"/>
 <comment>comments here</comment>
 <sender>sender's name</sender>
 <distribution>destination of this message</distribution>
 <organization>name of the organization where this message will be
distributed</organization>

 <update createuri="$ideas/$next"/>

 <keywords>keywords go here</keywords>
 <message whitespace="preserve">
  You message goes here. your <keyword>keywords</keyword> can be
highlighted.
 </message>
 <filingdate>12/31/2000</filingdate>  <!-- MM/DD/YYYY -->
 <cg:category main="general"/>
 <cg:category main="facility" sub="okc"/>
 <cg:category main="research" sub="gxos"/>
 <cg:category main="other" other="a_category"/>
 <cg:category main="other" sub="other" other="a_category/a_sub_category"/>
 <cg:messageType>WeeklyReport</cg:messageType>
 <cg:messageType>Other</cg:messageType>
</event>

</okc>





COMPLETE SOURCE CODE
---------------------------------------

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.net.*;
import java.io.*;

import org.xml.sax.InputSource;

import org.apache.xerces.parsers.DOMParser;

public class validate implements ErrorHandler {
  private String instance = null;
  private URL instanceURL = null;
  private boolean noSchema = false;
  private String schema = null;
  private int warnings = 0;
  private int errors = 0;
  private int fatalErrors = 0;
  private org.apache.xerces.parsers.DOMParser parser
                = new org.apache.xerces.parsers.DOMParser();
  public boolean valid = false;

  public validate()                 {   parser.setErrorHandler(this);   }
  public void setInstance(String s) {   instance = s;  }
  public void setInstance(URL u)    {   instanceURL = u;  }
  public void setSchema(String s)   {   schema = s; }
    //  System.out.println("Currently you must specify the schema utilizing"
+
    //  " xsi:schemaLocation " + "or xsi:noNamespaceSchemaLocation - using
default");



  public boolean doCheckSourceDocumentURL()throws Exception {

    try {
      try {
        // Turn validation off
        parser.setFeature( "http://xml.org/sax/features/validation", false);
        // Get the document
        System.out.println("");
        System.out.println("Checking source document...");

        parser.parse(new org.xml.sax.InputSource(new InputStreamReader(
this.instanceURL.openStream())) );
//        parser.parse(instance);

        // Get the document
        Document doc = parser.getDocument();
        if (doc != null) {
          // Check if the schema is null
          if (schema != null) {

            // Check for xsi:schemaLocation
            // Check for xsi:noNamespaceSchemaLocation !! assuming no
namespace for now

doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation",
schema);
            doc.getDocumentElement().setAttribute("xmlns:xsi",

org.apache.xerces.validators.schema.SchemaSymbols.URI_XSI);


          // Check if they specified the location
          } else if
((!doc.getDocumentElement().hasAttribute("xsi:schemaLocation"))
            &&
(!doc.getDocumentElement().hasAttribute("xsi:noNamespaceSchemaLocation"))) {

            this.noSchema = true;

            // Check for a namespace decl for the root element
            // Slam it for RDDL
            // Slam it for a Schema
            // No location - except out
//            throw new Exception("[Error] You must specify the schema for "
+ instance +
  //            " by including the attribute xsi:schemaLocation " +
    //          "or xsi:noNamespaceSchemaLocation on the root element");
          }
        }
      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
        return false;
      }
      // Made it through with no exceptions...
      return true;
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }




  public boolean doCheckSourceDocument() throws Exception {
    try {
      try {
        // Turn validation off
        parser.setFeature( "http://xml.org/sax/features/validation", false);
        // Get the document
        System.out.println("");
        System.out.println("Checking source document...");

    //    parser.parse(instance);

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

        // Get the document
        Document doc = parser.getDocument();
        if (doc != null) {
          // Check if the schema is null
          if (schema != null) {
            // Check for xsi:schemaLocation
            // Check for xsi:noNamespaceSchemaLocation !! assuming no
namespace for now

doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation",
schema);
            doc.getDocumentElement().setAttribute("xmlns:xsi",

org.apache.xerces.validators.schema.SchemaSymbols.URI_XSI);

          // Check if they specified the location
          } else if
((!doc.getDocumentElement().hasAttribute("xsi:schemaLocation")) &&

(!doc.getDocumentElement().hasAttribute("xsi:noNamespaceSchemaLocation"))) {

            this.noSchema = true;

            // Check for a namespace decl for the root element
            // Slam it for RDDL
            // Slam it for a Schema
            // No location - except out
//            throw new Exception("[Error] You must specify the schema for "
+ instance +
  //            " by including the attribute xsi:schemaLocation " +
    //          "or xsi:noNamespaceSchemaLocation on the root element");
          } // else if---
        }//else if---

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
        return false;
      }
      // Made it through with no exceptions...
      return true;
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }


  public boolean doValidate()throws Exception {

  if (this.noSchema) {
        System.out.println("There is no schema specified in the document, so
there will not be any validation.");
        this.noSchema = false;
        return false;
  }

    try {
      warnings = 0;
      errors = 0;
      fatalErrors = 0;
      try {
        // Now reparse with the validation turned on
        parser.setFeature( "http://xml.org/sax/features/validation", true);
        System.out.println("Validating source document...");

    //    parser.parse(instance);

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
      }

      // Return true if we made it this far with no errors
      System.out.println("");
      System.out.println("Complete " + warnings + " warnings " +
                        errors + " errors " + fatalErrors + " fatal
errors");
      return ((errors == 0) && (fatalErrors == 0));
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }

  public boolean doValidateURL() throws Exception{

  if (this.noSchema) {
        this.noSchema = false;
        System.out.println("There is no schema specified in the document, so
there will not be any validation.");
        return false;
  }

    try {
      warnings = 0;
      errors = 0;
      fatalErrors = 0;
      try {
        // Now reparse with the validation turned on
        parser.setFeature( "http://xml.org/sax/features/validation", true);
        System.out.println("Validating source document...");

        parser.parse(new org.xml.sax.InputSource(new InputStreamReader(
this.instanceURL.openStream())) );
        // parser.parse(instance);

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
      }

      // Return true if we made it this far with no errors
      System.out.println("");
      System.out.println("Complete " + warnings + " warnings " +
                        errors + " errors " + fatalErrors + " fatal
errors");
      return ((errors == 0) && (fatalErrors == 0));
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }

  public void warning(SAXParseException ex) {
     System.out.println("");
     System.err.println("[Warning] " + ex.getMessage() +
       " line " + ex.getLineNumber() + " column " + ex.getColumnNumber() +
       " - " + instance);
     warnings++;
  }

  public void error(SAXParseException ex) {
     System.out.println("");
     System.err.println("[Error] " + ex.getMessage() +
       " line " + ex.getLineNumber() + " column " + ex.getColumnNumber() +
       " - " + instance);
     errors++;
  }

  public void fatalError(SAXParseException ex) throws SAXException {
     System.out.println("");
     System.err.println("[Fatal Error] " + ex.getMessage() + " line " +
ex.getLineNumber() +
       " column " + ex.getColumnNumber() + " - " + instance);
     fatalErrors++;
//     throw ex;
  }


  public static void main(String[] args) {
    validate validate1 = new validate();
    if (args.length == 0) {
      System.out.println("Usage : java validate <instance> <schema>");
      return;
    }
    if (args.length >= 1) {           // Set the instance
      validate1.setInstance(args[0]);
    }
    if (args.length >= 2) {               // Set the Schema
      validate1.setSchema(args[1]);
    }
    // Validate - currently doCheckSourceDocument does nothing to modify the
    // validating document... it can be removed to speed this up...
//    if (!validate1.doCheckSourceDocument()) return;
//    if (!validate1.doValidate()) return;
  }
}








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