You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Markus Heinisch <Ma...@trivadis.com> on 2002/11/21 16:01:45 UTC
KEY-KEYREF validation
Hi,
I'm working with xerces 2.2.1. I try to validate an XML document with an
Key-Keyref XML Schema but my Java code (and xerces parser) does not find
the errors in my XML document. Element hr/depts/dept/DEPTNO contains the
key and hr/emps/emp/DEPTNO contains the keyref.
My question is: How do I have to configure xerces to validate the
document?
I am not interested in the DOMTree and I would like to avoid to
write/use additional helper classes. I just want to validate the
document fast.
Please, have a look at my code.
Any hints are welcome.
Thanks alot.
Markus Heinisch
---------------------------
XML DOC:
========
<?xml version="1.0"?>
<hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="hr.xsd">
<depts>
<dept>
<DEPTNO>10</DEPTNO> <!-- key -->
<DNAME>ACCOUNTING</DNAME>
</dept>
<dept>
<DEPTNO>10</DEPTNO> <!-- double key value-->
<DNAME>RESEARCH</DNAME>
</dept>
</depts>
<emps>
<emp>
<EMPNO>7499</EMPNO>
<ENAME>ALLEN</ENAME>
<DEPTNO>30</DEPTNO> <!-- does not ref. to a key value -->
</emp>
</emps>
</hr>
XML SCHEMA:
===========
<?xml version = '1.0'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="hr">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="depts">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="dept" minOccurs="0"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="DEPTNO"
type="xsd:integer" nillable="false" />
<xsd:element name="DNAME"
type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="emps">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="emp" minOccurs="0"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EMPNO"
type="xsd:integer" />
<xsd:element name="ENAME"
type="xsd:string" minOccurs="0"/>
<xsd:element name="DEPTNO"
type="xsd:integer" nillable="false"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="deptPK">
<xsd:selector xpath="depts/dept" />
<xsd:field xpath="DEPTNO" />
</xsd:key>
<xsd:keyref name="empDeptFK" refer="deptPK">
<xsd:selector xpath="emps/emp" />
<xsd:field xpath="DEPTNO" />
</xsd:keyref>
</xsd:element>
</xsd:schema>
MY CODE:
========
public static void main(String[] argv) throws Exception {
if (argv.length != 1) {
// Must pass in the name of the XML file.
System.err.println("Usage: java xmlp filename");
System.exit(1);
}
// get the parser factory
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance
();
factory.setAttribute
("http://apache.org/xml/features/validation/schema",
new Boolean(true));
//factory.setAttribute
("http://apache.org/xml/features/validation/schema-full-checking",
// new Boolean(true));
factory.setAttribute ("http://xml.org/sax/features/namespaces",
new Boolean(true));
// Get an instance of the parser
DocumentBuilder parser = factory.newDocumentBuilder ();
parser.setErrorHandler(new org.xml.sax.helpers.DefaultHandler(){
public void error(SAXParseException exception)
{System.out.println (exception);}
public void fatalError(SAXParseException
exception){System.out.println (exception);}
public void warning(SAXParseException
exception){System.out.println (exception);}
}
);
/* Several other configuration tests
---------------------------------
DOMParser parser = new DOMParser();
try {
parser.setFeature
("http://apache.org/xml/features/validation/schema",
true);
parser.setFeature
("http://apache.org/xml/features/validation/schema-full-checking",
true);
}
catch (SAXException e) {
System.err.println("could not set parser feature");
}
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.SAXParserFactoryImpl");
SAXParserFactory factory = SAXParserFactory.newInstance ();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
try {
reader.setFeature
("http://apache.org/xml/features/validation/schema",
true);
reader.setFeature
("http://apache.org/xml/features/validation/schema-full-checking",
true);
}
catch (SAXException e) {
System.err.println("could not set parser feature");
}
*/
// Generate a URL from the filename.
URL url = Util.getURLFromFile (argv[0]);
// Parse and obtain the document.
System.out.println ("parsing " + url.toString ());
Document doc = parser.parse(url.toString ());
/* parser.parse(new InputSource(url.toString ()),
new org.xml.sax.helpers.DefaultHandler());
*/ System.out.println ("done.");
}
}
------------------------------------------
Markus Heinisch
Dipl.-Inform.
Consultant
Trivadis GmbH
Freischützstrasse 92
81927 München
Germany
Tel.: +49-89-99275930
Fax : +49-89-99275959
Mobile: +49-172-7032261
mailto:markus.heinisch@trivadis.com
http://www.trivadis.com
trivadis Techno Circle