Michael,

Sorry. I was over excited. I was doing the same way as you suggested but my 
LSResourceResolver is not getting called which means that I am missing 
something which I am not aware of it. 

The Test2.xml which I attached in the previous mail, the tag "ns1:Document" 
contains namespace xmlns:ns1="urn:swift:xsd:$xtsn.998.001.01" which is on the 
2.xsd.

Please find the attached Code where I set LSresourceResolver to Validator.

Thanks
Ajay 
--- On Thu, 11/19/09, Michael Glavassevich <mrgla...@ca.ibm.com> wrote:

> From: Michael Glavassevich <mrgla...@ca.ibm.com>
> Subject: Re: xs:any namespace resolution
> To: j-users@xerces.apache.org
> Date: Thursday, November 19, 2009, 8:32 PM
> 
> (Ajay, patience. In the future, please don't
> post the same message over and over again to the list. We
> heard you the first time.)
> 
> 
> 
> Try writing an LSResourceResolver (which you would
> register with the validator) and have that return the 2nd
> schema document on the request with its target
> namespace.
> 
> 
> 
> Thanks.
> 
> 
> 
> Michael Glavassevich
> 
> XML Parser Development
> 
> IBM Toronto Lab
> 
> E-mail: mrgla...@ca.ibm.com
> 
> E-mail: mrgla...@apache.org
> 
> 
> 
> ajay bhadauria <abhadau...@yahoo.com> wrote on
> 11/19/2009 09:15:03 AM:
> 
> 
> 
> > Hi,
> 
> > 
> 
> > I do knot know much about xs:any construct and I
>  am new to this and
> 
> > I read it but I do not know how I can tell the
> validator to use 
> 
> > namespace (only runtime) which is defined in the
> different schema.
> 
> >  
> 
> > Using  
> 
> > <xs:any
> 
> > >  namespace="##any"
> processContents="strict"/> construct, 
> 
> > 
> 
> > I am vlidating xml instance with Validator which has a
> main schema 
> 
> > but validator does not know second schema.  
> 
> > 
> 
> > Now,  how I can tell the validator during
> validation time about the 
> 
> > second schema to which some elements belong to the
> namespace which 
> 
> > is defined in the second schema.   
> 
> > 
> 
> > Thanks
> 
> > ajay
> 
> > 
> 
> > --- On Thu, 11/19/09, ajay bhadauria
> <abhadau...@yahoo.com> wrote:
> 
> > 
> 
> > > From: ajay bhadauria
> <abhadau...@yahoo.com>
> 
> > > Subject: Re: xs:any namespace resolution
> 
> > > To: j-users@xerces.apache.org
> 
> > > Date: Thursday, November 19, 2009, 6:38 AM
> 
> > > Hi,
> 
> > > 
> 
> > > I am using <xs:any
> 
> > > > namespace="##any"
> processContents="strict"/> and I
> 
> > > am validating xml instance which is having
> elements which
> 
> > > belong to the different namespace and
> corresponding to this
> 
> > > namespace, schema is not pre-loaded. I mean yhat
> external
> 
> > > schema is not pre-loaded.
> 
> > > 
> 
> > > How do I tell Java Validator about this external
> schema so
> 
> > > that xml instance is validated successfully ?
> 
> > > 
> 
> > > 
> 
> > > Is there any way I can tell the validator at the
> time of
> 
> > > validation only about external schema which is
> not
> 
> > > pre-loaded with main schema?
> 
> > > 
> 
> > > Help me in understanding whether it can be done
> ???
> 
> > > Thanks
> 
> > > Ajay 
> 
> > > 
> 
> > > --- On Thu, 11/19/09, ajay bhadauria
> <abhadau...@yahoo.com>
> 
> > > wrote:
> 
> > > 
> 
> > > > From: ajay bhadauria
> <abhadau...@yahoo.com>
> 
> > > > Subject: xs:any namespace resolution
> 
> > > > To: j-users@xerces.apache.org
> 
> > > > Date: Thursday, November 19, 2009, 12:48 AM
> 
> > > > Hi,
> 
> > > > 
> 
> > > > I have schema file 1.xsd which has xs:any
> schema
> 
> > > construct
> 
> > > >  
> 
> > > > <xs:complexType
> name="ExtensionEnvelope">
> 
> > > >     
> 
> > >    <xs:sequence>
> 
> > > >         
> 
> > >    <xs:any
> 
> > > > namespace="##any"
> processContents="strict"/>
> 
> > > >     
> 
> > >    </xs:sequence>
> 
> > > >     </xs:complexType>
> 
> > > > 
> 
> > > > And I try to validate xml instance (
> test2.xml )
> 
> > > against
> 
> > > > this schema, it gave the following error
> 
> > > >      cvc-complex-type.2.4.c: The
> 
> > > > matching wildcard is strict, but no
> declaration can be
> 
> > > found
> 
> > > > for element 'ns1:Document'.
> 
> > > > 
> 
> > > > However, the namespace which the Validator
> is looking
> 
> > > for
> 
> > > > is in other xsd file (2.xsd). 
> 
> > > > 
> 
> > > > How do I tell Validator the location of
> 2.xsd file
> 
> > > during
> 
> > > > validation ? 
> 
> > > > 
> 
> > > > The test.xml has some elements which belong
> to the
> 
> > > > namespace urn:swift:xsd:$xtsn.998.001.01
> which is
> 
> > > defined in
> 
> > > > the 2.xsd file.
> 
> > > > 
> 
> > > > 
> 
> > > >       
> 
> > > >
> 
> > >
> ---------------------------------------------------------------------
> 
> > > > To unsubscribe, e-mail:
> j-users-unsubscr...@xerces.apache.org
> 
> > > > For additional commands, e-mail:
> j-users-h...@xerces.apache.org
> 
> > > 
> 
> > > 
> 
> > >       
> 
> > > 
> 
> > >
> ---------------------------------------------------------------------
> 
> > > To unsubscribe, e-mail:
> j-users-unsubscr...@xerces.apache.org
> 
> > > For additional commands, e-mail:
> j-users-h...@xerces.apache.org
> 
> >       
> 
> > 
> 
> >
> ---------------------------------------------------------------------
> 
> > To unsubscribe, e-mail:
> j-users-unsubscr...@xerces.apache.org
> 
> > For additional commands, e-mail:
> j-users-h...@xerces.apache.org
> 
>


      
import java.io.*;
import java.util.*;
import org.w3c.dom.* ;
import javax.xml.XMLConstants;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.Validator;
import javax.xml.parsers.DocumentBuilderFactory ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.ParserConfigurationException ;
import javax.xml.transform.dom.DOMSource;
import org.xml.sax.*  ;
import org.w3c.dom.ls.* ;
import org.xml.sax.InputSource;
import javax.xml.transform.stream.StreamSource ;

public class SchemaReader 
{

  private static int errorCount = 0;
  String schemaName = null ;
  MyResourceResolver resolver;

  public SchemaReader(String st, String st1)
  {
     schemaName = st;
     resolver = new MyResourceResolver();
     Schema schema =  loadSchema( schemaName );
     Validator validator = schema.newValidator();
     validator.setErrorHandler( new MyErrorHandler() ) ;
     validator.setResourceResolver( resolver );

      Document document= null;
      DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
      dFactory.setNamespaceAware(true);
      DocumentBuilder dBuilder =null;    
      try {
       dBuilder = dFactory.newDocumentBuilder();
      }
      catch( Exception e)
      {
          System.out.println(e.toString());
      }
      try
      {
            System.out.println("File Name : " + st1);
            document = dBuilder.parse( new FileInputStream(st1));
      }
      catch( Exception e)
      {
          System.out.println(e.toString());
      }

      DOMSource source = new DOMSource(document);

     try
     {
       validator.validate(source);
       
     }
     catch (Exception e)
     {
          System.out.println("In the Validation Exception");
          System.out.println(e.toString());
          //e.printStackTrace(System.out);
          System.exit(1);
     }
     System.out.println("Document is good");
  }

  public static void main(String[] a) 
  {
     if (a.length<2) 
     {
          System.out.println("Usage:");
          System.out.println("java XsdSchemaValidator schema_file_name " + 
"xml_file_name");
     } 
     else 
     {
         SchemaReader sr = new SchemaReader(a[0],a[1]);
     }
   }

  public Schema loadSchema(String name) 
  {
    Schema schema = null;
    try 
    {
      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      MyErrorHandler handler = new MyErrorHandler();
      SchemaFactory factory = SchemaFactory.newInstance(language);
      factory.setErrorHandler(handler);
      factory.setResourceResolver( resolver );
      System.out.println("Schema File Name : " +  name);
      schema = factory.newSchema(new File(name));

    } 
    catch (Exception e) 
    {
      System.out.println(e.toString());
      System.out.println("debug2 ");
    }
     return schema;
  }


 public class MyErrorHandler implements ErrorHandler
{

      public void error(SAXParseException exception )
      {
             
              System.out.println("In the error");
              System.out.println(exception.getMessage());
              System.out.println(exception.getLineNumber());

       }
      public void fatalError(SAXParseException exception )
      {

              System.out.println(exception.toString());
      }
      public void warning(SAXParseException exception )
      {
            
              System.out.println("Warning :");
              System.out.println(exception.toString());
      }

}

  public  class MyResourceResolver implements LSResourceResolver 
  {

           public LSInput resolveResource(String type, String namespaceURI, 
String publicId, String systemId, String baseURI ) 
           {
                 System.out.println("System Id:" +  systemId);
                 System.out.println("namespaceURI " +  namespaceURI);
                 System.out.println("baseURI:" +  baseURI);
                 String path = "/tmp/ajay/xslt/" + systemId;
                 System.out.println("File Path " +  path);
                 ResourceInput ri = new ResourceInput(path);
                 return ri;
           }
  }

  private static class ResourceInput implements LSInput {

    private final String resource;

    private ResourceInput(String resource) {
      this.resource = resource;
    }

    public Reader getCharacterStream()
    {
        Reader schemaReader   = null ;
        schemaReader =  (new StreamSource(new File( resource ))).getReader() ;
        return schemaReader ;
    }

    public void setCharacterStream(Reader characterStream) {}
    public InputStream getByteStream() { return null; }
    public void      setByteStream(InputStream byteStream){}
    public String    getStringData() {return null ;}
    public void      setStringData(String stringData) {}
    public String    getSystemId() { return this.resource ; }
    public void      setSystemId(String systemId) {}
    public String    getPublicId() {return null ;}
    public void      setPublicId(String publicId) {}
    public String    getBaseURI() {return null ;}
    public void      setBaseURI(String baseURI) {}
    public String    getEncoding() {return null ;}
    public void      setEncoding(String encoding) {}
    public boolean   getCertifiedText() {return false ;}
    public void      setCertifiedText(boolean certifiedText) {}
  }

}
---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org
For additional commands, e-mail: j-users-h...@xerces.apache.org

Reply via email to