xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mandy.s...@uk.catalyst-solutions.com
Subject Re: XML Schema validation and default attributes
Date Fri, 05 Jan 2001 18:29:38 GMT
Simon,

I don't know if it's any help but I had somewhat similar trouble validating
against a DTD, and I found that validation only seemed to work at all when
I included error handling code, borrowed off the Web somewhere (can't
remember where now). Code bits and pieces follow (N.B. I am not very good
at Java so please don't laugh.)

******...

public class domValidate
  implements ErrorHandler
{

  // Main processing
  public void parseAndPrint(String uri)
  {

    // DOM tree workspace
    Document doc = null;

    try
    {
      // instantiate parser
      DOMParser parser = new DOMParser();
      // make it log messages at the required level
      parser.setErrorHandler(this);
      // make it validate against DTD (essential)
      parser.setFeature("http://xml.org/sax/features/validation", true);
      // parse our XML file
      parser.parse(uri);
      // point DOM tree workspace at the parser's output
      doc = parser.getDocument();
    }
    catch (org.xml.sax.SAXParseException spe) {
      System.err.println("** Sorry, a fatal SAX parse error occurred - see
previous messages");
      System.exit(-1);
    }
    catch (org.xml.sax.SAXNotRecognizedException ex ){
      System.err.println("** Sorry, a SAX Not Recognized error occurred: "
+ ex);
      System.exit(-1);
    }
    catch (org.xml.sax.SAXNotSupportedException ex ){
      System.err.println("** Sorry, a SAX Not Supported error occurred: " +
 ex);
      System.exit(-1);
    }
    catch (org.xml.sax.SAXException se) {
      System.err.println("** Sorry, a SAX error occurred: " + se);
      System.exit(-1);
    }
    catch (Exception e)
    {
      System.err.println("** Sorry, an error occurred: " + e);
      System.exit(-1);
    }

...
  }

...
  // Error handling code for implementing ErrorHandler

  /** Warning. */
  public void warning(SAXParseException ex) {
      System.err.println("[Warning] "+
                         getLocationString(ex)+": "+
                         ex.getMessage());
  }

  /** Error. */
  public void error(SAXParseException ex) {
      System.err.println("[Error] "+
                         getLocationString(ex)+": "+
                         ex.getMessage());
  }

  /** Fatal error. */
  public void fatalError(SAXParseException ex) throws SAXException {
      System.err.println("[Fatal Error] "+
                         getLocationString(ex)+": "+
                         ex.getMessage());
      throw ex;
  }

  // Private code to get a few more diagnostics in case of parsing
problems.
  private String getLocationString(SAXParseException ex)
  {
    StringBuffer str = new StringBuffer();
    String systemId = ex.getSystemId();
    if (systemId != null)
      {
      int index = systemId.lastIndexOf('/');
      if (index != -1) systemId = systemId.substring(index + 1);
      str.append(systemId);
      }
    str.append(':');
    str.append(ex.getLineNumber());
    str.append(':');
    str.append(ex.getColumnNumber());

    return str.toString();

  } // getLocationString

}






"Simon Tyler" <tylersimon@hotmail.com> on 01/05/2001 03:31:02 PM

Please respond to general@xml.apache.org

To:   general@xml.apache.org, xerces-j-dev@xml.apache.org
cc:    (bcc: Mandy Shaw/Pacific/UK)
Subject:  XML Schema validation and default attributes





Hi,

I've been trying to use Xerces-j 1.2.3 with XML Schemas and I'm having a
few
problems. Sorry if this has been covered before but I couldn't find
anything
in the archives.

I'm trying to read some XML into a DOM validating it as I go. I would like
the DOM to include any attribute defaults that are set in the schema. I'm
having two problems:

1. Validation doesn't seem to occur. I can enter invalid elements and the
parser seems to be happy with it. Tools like XMLSpy complain about the
invalid element.

2. When I look at the DOM that was produced none of the attribute defaults
get inserted, only attributes that exist in the original document.

Included below is an example XML document, XML Schema and a source snippet
of the parser options I'm using. I know the examples use the "2000/10"
standard but this is for XMLSpy, repplacing with "1999" makes no
difference.

Thanks for any help. Could you reply directly as well as to the list.

Simon

--- XML Schema ---

<?xml version = "1.0" encoding = "UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
<xsd:element name="attrtest">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="myelement">
     <xsd:complexType>
      <xsd:sequence/>
      <xsd:attribute name = "attr1" use = "default" value = "one" type =
"xsd:string"/>
      <xsd:attribute name = "attr2" use = "default" value = "two" type =
"xsd:string"/>
      </xsd:complexType>
     </xsd:element>
    </xsd:sequence>
   </xsd:complexType>
</xsd:element>
</xsd:schema>

--- XML Document ---

<?xml version="1.0" encoding="UTF-8"?>
<attrtest xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="attrtest.xsd">
  <myelement attr1="test"/>
</attrtest>

--- Code ---

DOMParser parser = new DOMParser();

try
{
  parser.setFeature("http://xml.org/sax/features/validation",true );

  parser.parse(filename);
}
catch etc. etc.

_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


---------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org




      Regards,
      Mandy Shaw

      Catalyst Solutions plc
      Kingfisher House
      Frimley Business Park
      Camberley
      Surrey
      GU16 5SG
      UK

      http://www.catalyst-solutions.com
      Email: Mandy.Shaw@uk.catalyst-solutions.com

      Telephone: +44 (0)870 166 1000
      DDI: +44 870 166 1324
      Facsimile: +44 870 168 3920
      Mobile: +44 410 447966





----------------------------------------------------------------------
----------------------------------------------------------------------
Catalyst Solutions plc.  Registered No 2918101.
Registered @ Kingfisher House, Frimley Business Park, Frimley,
Surrey. GU16 5SG   U.K.

NOTICE:
This message is intended only for the named addressee(s) and may
contain confidential and/or privileged information. If you are not the
named addressee you should not disseminate, copy or take any action
or place any reliance on it. If you have received this message in error
please notify postmaster@catalyst-solutions.com and delete the message
and any attachments accompanying it immediately.
----------------------------------------------------------------------



Mime
View raw message