473,226 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Xerces 2.6 and validation of XML schema constraints

Apprentently xerces 2.6.0 (Java) does not validate against contraints
specified in the schema (e.g. constraints specified via unique element).

The validation works with the XML editor I'm using (XMLSpy4) but not
with Xerces 2.6.0.

I've included a really short and simple example to illustrate it. I
would like to get some comments on the validation capabilities of Xerces
2.6.0. I though it *fully* supported W3C Schema!

Here's the XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.mynames.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mynames.org
x:/work/sjrodc/xml/constraint.xsd">
<object id="1"/>
<!-- Here's the uniqueness violation -->
<object id="1"/>
</root>

Here's the Schema file. It defines a uniqueness constraint on the
object's 'id' attribute.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.mynames.org"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.mynames.org" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="object" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="objectId">
<xs:selector xpath=".//object"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
</xs:schema>

And here's the Java code that does the parsing:
import java.io.File;
import java.io.IOException;

import java.text.MessageFormat;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlValidationTest
{
public static String SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
public static String XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
public static String SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";

public static final void main( String[] args )
throws IOException, SAXException, ParserConfigurationException
{
if( args.length < 2 )
{
System.err.println( "usage is:" );
System.err.println( " <xml file> <schema file>" );

return;
}

File input = new File( args[0] );
File schema = new File( args[1] );
SAXParserFactory factory = SAXParserFactory.newInstance( );
factory.setNamespaceAware( true );
factory.setValidating( true );

SAXParser parser = factory.newSAXParser( );

try
{
parser.setProperty( SCHEMA_LANGUAGE, XML_SCHEMA );
parser.setProperty( SCHEMA_SOURCE, schema );
}
catch( SAXNotRecognizedException x )
{
System.err.println( "Your SAX parser is not JAXP 1.2
compliant." );
}

System.out.println( "Parsing '" + args[0] +
"' against W3C schema '" +
args[1] + "' ..." );

parser.parse( input, new ErrorPrinter( ) );

System.out.println( "Done." );
}

static class ErrorPrinter
extends DefaultHandler
{
private MessageFormat message =
new MessageFormat( "({0}: {1}, {2}): {3}" );

private void print( SAXParseException x )
{
String msg =
message.format( new Object[]
{
x.getSystemId( ),
new Integer( x.getLineNumber( ) ),
new Integer( x.getColumnNumber( ) ),
x.getMessage( )
} );
System.out.println( msg );
}
public void warning( SAXParseException x )
{
print( x );
}
public void error( SAXParseException x )
{
print( x );
}
public void fatalError( SAXParseException x )
throws SAXParseException
{
print( x );
throw x;
}
}
}
Jul 20 '05 #1
2 4620


Olaf Meyer wrote:
Apprentently xerces 2.6.0 (Java) does not validate against contraints
specified in the schema (e.g. constraints specified via unique element).

The validation works with the XML editor I'm using (XMLSpy4) but not
with Xerces 2.6.0.

I've included a really short and simple example to illustrate it. I
would like to get some comments on the validation capabilities of Xerces
2.6.0. I though it *fully* supported W3C Schema!

Here's the XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.mynames.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mynames.org
x:/work/sjrodc/xml/constraint.xsd">
It could be a URL issue, what is
x:/work...
that doesn't look like a URL to me.
Maybe you want
file:///x:/work...
<object id="1"/>
<!-- Here's the uniqueness violation -->
<object id="1"/>
</root>

Here's the Schema file. It defines a uniqueness constraint on the
object's 'id' attribute.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.mynames.org"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.mynames.org" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="object" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="objectId">
<xs:selector xpath=".//object"/>


But more likely it is an XPath namespace issue, XPath 1.0 doesn't know a
default namespace, as your elements are in the namespace
http://www.mynames.org I suggest you declare a prefix for that e.g.
<xs:schema xmlns:mynamespace="http://www.mynames.org" ...>
and use that prefix in the XPath expression e.g.
<xs:selector xpath=".//mynamespace:object"/>
When I do that with your example schema and then check the XML instance
file in jEdit (which uses Xerces Java) it indeed flags an error

test20040115.xml:5:Duplicate unique value [ID Value: 1] declared for
identity constraint of element "root".


--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Martin Honnen wrote:


Olaf Meyer wrote:
Apprentently xerces 2.6.0 (Java) does not validate against contraints
specified in the schema (e.g. constraints specified via unique element).

The validation works with the XML editor I'm using (XMLSpy4) but not
with Xerces 2.6.0.

I've included a really short and simple example to illustrate it. I
would like to get some comments on the validation capabilities of
Xerces 2.6.0. I though it *fully* supported W3C Schema!

Here's the XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.mynames.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mynames.org
x:/work/sjrodc/xml/constraint.xsd">

It could be a URL issue, what is
x:/work...
that doesn't look like a URL to me.
Maybe you want
file:///x:/work...
<object id="1"/>
<!-- Here's the uniqueness violation -->
<object id="1"/>
</root>

Here's the Schema file. It defines a uniqueness constraint on the
object's 'id' attribute.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.mynames.org"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.mynames.org" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="object" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="objectId">
<xs:selector xpath=".//object"/>

But more likely it is an XPath namespace issue, XPath 1.0 doesn't know a
default namespace, as your elements are in the namespace
http://www.mynames.org I suggest you declare a prefix for that e.g.
<xs:schema xmlns:mynamespace="http://www.mynames.org" ...>
and use that prefix in the XPath expression e.g.
<xs:selector xpath=".//mynamespace:object"/>
When I do that with your example schema and then check the XML instance
file in jEdit (which uses Xerces Java) it indeed flags an error

test20040115.xml:5:Duplicate unique value [ID Value: 1] declared for
identity constraint of element "root".


Martin,

thanks alot, this solve the problem ;-)

Olaf
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: BODIN | last post by:
I have an XML document, which is actually physically stored in 2 separated files. doc1.xml and doc2.xml I NEED a WAY FOR THIS SIMPLE NEED : XML file splitted into two physical files, and use...
4
by: joes | last post by:
Hello there I tried for several days to get a simple validation with xml schema & xerces working. Goal for me is tuse JAXP and not specific Xerces classes. I don't get the point what I am doing...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
5
by: Harald Haspl | last post by:
Hello, I've appended a simple XML file and it's appropriate schema below. I want to validate the XML file against the schema with xerces-c. This example contains a section where colours are...
3
by: ciaran.mchale | last post by:
Hi folks, I downloaded the binary version of Xerces C++ 2.7.0 for Windows and am using it to help me get up to speed with XML and XML Schema. So please excuse me if this is a "novice" question....
7
by: christian.eickhoff | last post by:
Hi Everyone, I am currently implementing an XercesDOMParser to parse an XML file and to validate this file against its XSD Schema file which are both located on my local HD drive. For this...
1
by: aralvarez_nospam_at_gmail.com change _nospam_at_ | last post by:
I have been trying to register some HL7 schemas into DB2-Express -C v9. After invoking the following command: complete xmlschema "datatypes-base"; it throws me the following error: ...
9
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13>...
2
by: Boris Kolpackov | last post by:
Hi, I am pleased to announce the availability of Apache Xerces-C++ 3.0.0. Xerces-C++ is an open-source validating XML parser written in a portable subset of C++. It provides DOM (level 1, 2, and...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.