Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Xerces 2.6 and validation of XML schema constraints

Reply
Thread Tools

Xerces 2.6 and validation of XML schema constraints

 
 
Olaf Meyer
Guest
Posts: n/a
 
      01-15-2004
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"
xmlnssi="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"
xmlnss="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;
}
}
}
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-15-2004


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"
> xmlnssi="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"
> xmlnss="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=".//mynamespacebject"/>
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:5uplicate unique value [ID Value: 1] declared for
identity constraint of element "root".




--

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

 
Reply With Quote
 
 
 
 
Olaf Meyer
Guest
Posts: n/a
 
      01-15-2004
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"
>> xmlnssi="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"
>> xmlnss="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=".//mynamespacebject"/>
> 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:5uplicate unique value [ID Value: 1] declared for
> identity constraint of element "root".
>


Martin,

thanks alot, this solve the problem

Olaf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. Bobby Edward ASP .Net 4 11-03-2008 10:53 PM
Gridview error: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. Bobby Edward ASP .Net 1 10-14-2008 04:09 AM
Validation with XSD using XML::LibXML::Schema, and XML::Validator::Schema huntingseasonson@gmail.com Perl Misc 5 11-29-2006 12:37 PM
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. bazzer ASP .Net 1 04-06-2006 01:31 PM
Newbie problem?: XML-Schema Instance Validation (xerces 2.6.0, confusingcvc-complex-type errors) Grand Apeiron XML 7 01-20-2004 12:04 AM



Advertisments