Hi Simon,

You weren't actually using Xerces when you ran your test. The 
com.sun.org.apache.xerces.internal.* packages originate from Sun's Java 5. 
Regardless of whether there are schema locations in the document or not it 
doesn't seem to call the LSResourceResolver. This is probably one of many 
bugs which we've fixed (in the last three years) or one that never existed 
since the Validator implementation in Xerces is significantly different 
than the one included in Java 5.

There's one Java 5 bug [1] in particular which prevents the 
SchemaFactory's factory finder from finding the implementation on the 
classpath. If you had xercesImpl.jar in your classpath this would explain 
why you got the JDK default instead. To select Xerces' SchemaFactory you 
can set a system property -Djavax.xml.validation.SchemaFactory:
http://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory
. Putting the xml-apis.jar which accompanies Xerces in your JDK's endorsed 
directory [2] works as well.

Once you get it working the output from your test (with the print out from 
your LSResourceResolver) should look something like:

http://www.w3.org/2001/XMLSchema / http://example.com/root / null / null / 
file:///C:/xmldocs/test.xml
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of 
element 'root'.
        at 
org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:196)
        at 
org.apache.xerces.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:132)
        at 
org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:347)
        at 
org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:281)
        at 
org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2011)
        at 
org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:725)
        at 
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:283)
        at 
org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(XMLNSDocumentScannerImpl.java:736)
        at 
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1755)
        at 
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:324)
        at 
org.apache.xerces.parsers.XML11Configuration.parse(XML11Configuration.java:845)
        at 
org.apache.xerces.parsers.XML11Configuration.parse(XML11Configuration.java:768)
        at 
org.apache.xerces.jaxp.validation.StreamValidatorHelper.validate(StreamValidatorHelper.java:121)
        at 
org.apache.xerces.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:110)
        at javax.xml.validation.Validator.validate(Unknown Source)
        at SchemaTest.main(SchemaTest.java:20)

Thanks.

[1] http://marc.theaimsgroup.com/?l=xml-commons-dev&m=111898640501739&w=2
[2] http://xerces.apache.org/xerces2-j/faq-general.html#faq-4

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]

Simon Raess <[EMAIL PROTECTED]> wrote on 09/26/2006 12:08:38 AM:

> Hi
> 
> I've tried to use it, but the LSResourceResolver was never called:
> 
> public class SchemaTest {
>    public static void main(String[] args) throws Exception {
>      SchemaFactory factory = SchemaFactory.newInstance 
> (XMLConstants.W3C_XML_SCHEMA_NS_URI);
>      Schema schema = factory.newSchema();
>      Validator validator = schema.newValidator();
>      validator.setResourceResolver(new Resolver());
>      validator.validate(new StreamSource(new File(args[0])));
>    }
> 
>    private static class Resolver implements LSResourceResolver {
>       public LSInput resolveResource(
>           java.lang.String type, java.lang.String namespaceURI, 
> java.lang.String publicId, java.lang.String systemId, 
> java.lang.String baseURI) {
>           System.out.println(type + " / " + namespaceURI + " / " + 
> publicId + " / " + systemId + " / " + baseURI);
>           return null;
>       }
>    }
> }
> 
> I haven't used the Validator stuff before. Do I have to configure 
> something more? My test xml looks like:
> 
> <root xmlns="http://example.com/root";>
>     <child>test</child>
> </root>
> 
> And the test program fails with the following exception:
> 
> Exception in thread "main" org.xml.sax.SAXParseException: cvc-elt.1: 
> Cannot find the declaration of element 'root'.
>          at 
> com.sun.org.apache.xerces.internal.jaxp.validation.Util.toSAXParseExcept 

> ion(Util.java:109)
>          at 
> com.sun.org.apache.xerces.internal.jaxp.validation.ErrorHandlerAdaptor.e 

> rror(ErrorHandlerAdaptor.java:104)
>          at 
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError 
> (XMLErrorReporter.java:382)
>          at 
> com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError 
> (XMLErrorReporter.java:316)
>          at 
> com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStar 

> tElement(XMLSchemaValidator.java:1944)
>          at 
> com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startEleme 

> nt(XMLSchemaValidator.java:705)
>          at 
> com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl. 

> startElement(ValidatorHandlerImpl.java:335)
>          at 
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElemen 

> t(AbstractSAXParser.java:533)
>          at 
> com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanSta 

> rtElement(XMLNSDocumentScannerImpl.java:330)
>          at 
> com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl 
> $NSContentDispatcher.scanRootElementHook 
> (XMLNSDocumentScannerImpl.java:779)
>          at 
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl 
> $FragmentContentDispatcher.dispatch 
> (XMLDocumentFragmentScannerImpl.java:1794)
>          at 
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.s 

> canDocument(XMLDocumentFragmentScannerImpl.java:368)
>          at 
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse 
> (XML11Configuration.java:834)
>          at 
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse 
> (XML11Configuration.java:764)
>          at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse 
> (XMLParser.java:148)
>          at 
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse 
> (AbstractSAXParser.java:1242)
>          at 
> com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.process 

> (ValidatorImpl.java:192)
>          at 
> com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validat 

> e(ValidatorImpl.java:159)
>          at javax.xml.validation.Validator.validate(Validator.java:82)
>          at SchemaTest.main(SchemaTest.java:14)
> 
> Simon
> 
> 
> 
> On 25.09.2006, at 23:06, Dick Deneer wrote:
> 
> > Can't you use the LSResourceResolver in combination with the JAXP 
> > Validator.
> >
> > Dick Deneer
> > Op 25-sep-2006, om 22:22 heeft Simon Raess het volgende geschreven:
> >
> >> Hi
> >>
> >> I'm developing an XML format that is extensible. The format serves 
> >> as a kind of container with well-defined extension points. The 
> >> following example shows a document that is similar to the format 
> >> I'm using:
> >>
> >>  <container xmlns="urn:container">
> >>    <binding name="x"/>
> >>    <access xmlns="urn:xpath">...</access>
> >>  </container>
> >>
> >> I want to validate the above XML against the corresponding 
> >> schemas. However, I don't want to specify the schema locations 
> >> inside the document (I don't trust the creators of the document). 
> >> I'd like to look up the schemas at runtime based on the namespace 
> >> attribute. Unfortunately, I haven't been able to figure out how to 
> >> tell Xerces which schema to use to validate the document.
> >>
> >> I know, I could specify all the schema locations programatically. 
> >> But I don't want to do that. I'd rather have Xerces ask me on 
> >> demand how to resolve the namespace to a schema document (kind of 
> >> like an EntityResolver but based on the namespace).
> >>
> >> Is this approach possible with Xerces? If yes, how?
> >>
> >> Thanks
> >> Simon
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: [EMAIL PROTECTED]
> >> For additional commands, e-mail: [EMAIL PROTECTED]
> >>
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to