Xerces 2.6 and validation of XML schema constraints

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olaf Meyer

    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:schemaLocat ion="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 " elementFormDefa ult="qualified" >
    <xs:element name="root">
    <xs:complexType >
    <xs:sequence>
    <xs:element name="object" maxOccurs="unbo unded">
    <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.IOExcep tion;

    import java.text.Messa geFormat;

    import javax.xml.parse rs.ParserConfig urationExceptio n;
    import javax.xml.parse rs.SAXParser;
    import javax.xml.parse rs.SAXParserFac tory;

    import org.xml.sax.SAX Exception;
    import org.xml.sax.SAX NotRecognizedEx ception;
    import org.xml.sax.SAX ParseException;
    import org.xml.sax.hel pers.DefaultHan dler;


    public class XmlValidationTe st
    {
    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, ParserConfigura tionException
    {
    if( args.length < 2 )
    {
    System.err.prin tln( "usage is:" );
    System.err.prin tln( " <xml file> <schema file>" );

    return;
    }

    File input = new File( args[0] );
    File schema = new File( args[1] );
    SAXParserFactor y factory = SAXParserFactor y.newInstance( );
    factory.setName spaceAware( true );
    factory.setVali dating( true );

    SAXParser parser = factory.newSAXP arser( );

    try
    {
    parser.setPrope rty( SCHEMA_LANGUAGE , XML_SCHEMA );
    parser.setPrope rty( SCHEMA_SOURCE, schema );
    }
    catch( SAXNotRecognize dException x )
    {
    System.err.prin tln( "Your SAX parser is not JAXP 1.2
    compliant." );
    }

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

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

    System.out.prin tln( "Done." );
    }

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

    private void print( SAXParseExcepti on x )
    {
    String msg =
    message.format( new Object[]
    {
    x.getSystemId( ),
    new Integer( x.getLineNumber ( ) ),
    new Integer( x.getColumnNumb er( ) ),
    x.getMessage( )
    } );
    System.out.prin tln( msg );
    }


    public void warning( SAXParseExcepti on x )
    {
    print( x );
    }


    public void error( SAXParseExcepti on x )
    {
    print( x );
    }


    public void fatalError( SAXParseExcepti on x )
    throws SAXParseExcepti on
    {
    print( x );
    throw x;
    }
    }
    }
  • Martin Honnen

    #2
    Re: Xerces 2.6 and validation of XML schema constraints



    Olaf Meyer wrote:
    [color=blue]
    > 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:schemaLocat ion="http://www.mynames.org
    > x:/work/sjrodc/xml/constraint.xsd" >[/color]

    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...
    [color=blue]
    > <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 " elementFormDefa ult="qualified" >
    > <xs:element name="root">
    > <xs:complexType >
    > <xs:sequence>
    > <xs:element name="object" maxOccurs="unbo unded">
    > <xs:complexType >
    > <xs:attribute name="id"/>
    > </xs:complexType>
    > </xs:element>
    > </xs:sequence>
    > </xs:complexType>
    > <xs:unique name="objectId" >
    > <xs:selector xpath=".//object"/>[/color]

    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:mynamespa ce="http://www.mynames.org " ...>
    and use that prefix in the XPath expression e.g.
    <xs:selector xpath=".//mynamespace:obj ect"/>
    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.xm l:5:Duplicate unique value [ID Value: 1] declared for
    identity constraint of element "root".




    --

    Martin Honnen


    Comment

    • Olaf Meyer

      #3
      Re: Xerces 2.6 and validation of XML schema constraints

      Martin Honnen wrote:[color=blue]
      >
      >
      > Olaf Meyer wrote:
      >[color=green]
      >> 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:schemaLocat ion="http://www.mynames.org
      >> x:/work/sjrodc/xml/constraint.xsd" >[/color]
      >
      >
      > 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...
      >[color=green]
      >> <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 " elementFormDefa ult="qualified" >
      >> <xs:element name="root">
      >> <xs:complexType >
      >> <xs:sequence>
      >> <xs:element name="object" maxOccurs="unbo unded">
      >> <xs:complexType >
      >> <xs:attribute name="id"/>
      >> </xs:complexType>
      >> </xs:element>
      >> </xs:sequence>
      >> </xs:complexType>
      >> <xs:unique name="objectId" >
      >> <xs:selector xpath=".//object"/>[/color]
      >
      >
      > 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:mynamespa ce="http://www.mynames.org " ...>
      > and use that prefix in the XPath expression e.g.
      > <xs:selector xpath=".//mynamespace:obj ect"/>
      > 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.xm l:5:Duplicate unique value [ID Value: 1] declared for
      > identity constraint of element "root".
      >[/color]

      Martin,

      thanks alot, this solve the problem ;-)

      Olaf

      Comment

      Working...