You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Burris, Eddie" <bu...@umkc.edu> on 2002/07/29 07:24:19 UTC

How to specify multiple schema locations within program

I'm using xerces 2.0.2 and according to XML tutorial at:

http://java.sun.com/webservices/docs/1.0/tutorial/doc/JAXPDOM9.html

It's possible to specify multiple schemas within your program by setting the schemaSource property to an array of schema sources. However, the following program fails at runtime. I think the problem starts in XMLSchemaLoader.java.

Eddie


// ***MultiSchemaSpecifiedInProgram.java ****
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.File;
import org.w3c.dom.*;

public class MultiSchemaSpecifiedInProgram {
	static final String JAXP_SCHEMA_LANGUAGE =
		"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
	
	static final String W3C_XML_SCHEMA =
		"http://www.w3.org/2001/XMLSchema";

	static final String ContainerSchema  = "container.xsd";
	static final String ItemsSchema      = "items.xsd";

	static final String[] schemas = {
	        ContainerSchema,
			ItemsSchema
    };
	
	static final String JAXP_SCHEMA_SOURCE =
		"http://java.sun.com/xml/jaxp/properties/schemaSource";

    public static void main(String argv[]) {
		Document document;
        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
		factory.setValidating(true);
        factory.setNamespaceAware(true);
		try {
			factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
			factory.setAttribute(JAXP_SCHEMA_SOURCE, schemas);
		} 
		catch (IllegalArgumentException e) {
			System.out.println(e);
			e.printStackTrace();
		}

        try {
           DocumentBuilder builder = factory.newDocumentBuilder();
           document = builder.parse( new File("instance.xml") );
        } catch (Exception e) {
			System.out.println(e);
			e.printStackTrace();
        }
	}
}

******** instance.xml **********
<?xml version="1.0" encoding="ISO-8859-1"?>
<c:container xmlns:c="urn:container"
         xmlns:i="urn:items"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <i:item>First item</i:item>
  <i:item>Second item</i:item>
</c:container>

******** container.xsd **********
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:container">

  <xs:element name="container">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

******** items.xsd **********
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="urn:items">

  <xs:element name="item" type="xs:string" />

</xs:schema>

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org