Hi Volker :)
The problem here is that you're not setting the SystemID on the InputSource
you're getting back.
In other words, you should change
return new InputSource(findSchemaInstance(filename));
to
InputSource source = new InputSource(findSchemaInstance(filename));
source.setSystemId(systemID);
return source;
I had this problem with an EntityResolver that was resolving entities based
on a fake protocol I came up with, "classpath://" -- as you might guess, it
searched the classpath for a resource before giving up and delegating to
the default resolver. Anyway, the point being that the new InputSource
object you create still needs to keep track of its SystemID, which is lost
when you create a new one and return it right away.
Hope this helps,
Constantine
To: [EMAIL PROTECTED]
Volker Witzel cc: (bcc: Constantine
Georges/Towers Perrin)
<[EMAIL PROTECTED] Subject: Custom EntityResolver
and xs:include not working
henker.com>
07/30/2003 05:38
AM
Please respond to
xerces-j-user
Dear all,
I have a problem with validating an XML string programmtically against a
schema witch includes other schemas without a namespace.
A custom Entityresolver is put in place to find the schema instances.
Please see attached files for saample XSD and a sample XML, which is
used for JUnit testing.
The environment is as follows:
user.xsd includes both smart-types.xsd and company.xsd
user.xsd can be validated within WSAD 5, so it there finds and validates
the external resources correctly.
This makes me quite sure that the XSDs are fine.
The Java/Xerces Part:
I do try to convert an XML InputSource to a Document with validation.
sample code:
public static Document convertToDocument(InputSource in, String
xmlSchemaSource) throws TechnicalException {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = null;
if (!StringHelper.isNullOrEmpty(xmlSchemaSource))
{
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
factory.setAttribute(JAXP_SCHEMA_SOURCE, xmlSchemaSource);
builder = factory.newDocumentBuilder
();
builder.setErrorHandler(new
SWFXMLErrorHandler());
builder.setEntityResolver(new
XSDEntityResolver());
}
return builder.parse(in);
and sample code from XSDEntityResolver:
public InputSource resolveEntity(String publicID, String systemID)
throws IOException, SAXException {
if (systemID != null && systemID.endsWith(".xsd")) {
String filename =
systemID.substring(systemID.lastIndexOf(GlobalConstants.SEPARATOR_CHAR)
+ 1);
return new
InputSource(findSchemaInstance(filename));
}
return null; //important !
}
The method "resolveEntity" is called three times (for each xsd file),
each finding the correct XSD file and returning a valid Input source, so
Xerces seems to parse the user.xsd correctly. So far so good, but then
the following excpetion is thrown:
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name
'UserId' to a(n) type definition component.
at
org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown
Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown
Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDAttributeTraverser.traverseNamedAttr(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDAttributeTraverser.traverseLocal(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDAbstractTraverser.traverseAttrsAndAttrGrps(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.processComplexContent(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDComplexTypeTraverser.traverseGlobal(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDHandler.traverseSchemas(Unknown
Source)
at
org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown
Source)
at
org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at
org.apache.xerces.impl.xs.XMLSchemaLoader.processJAXPSchemaSource(Unknown
Source)
at
org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at
org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown
Source)
at
org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown
Source)
at
org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown
Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown
Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown
Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown
Source)
at
com.schenker.swf.helper.DomUtils.convertToDocument(DomUtils.java:321)
at
com.schenker.swf.helper.DomUtils.convertToDocument(DomUtils.java:283)
at
com.schenker.dbsmart.authentication.datastructures.test.UserTest.setUp(UserTest.java:46)
I don't have a clue what's wrong. Perhaps a bug in Xerces? I already
digged all appropriate newsgroups, without success. So you are my last
resort.
Any help or hints greatly appreciated!
Volker.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="String24">
<xs:restriction base="xs:token">
<xs:maxLength value="24"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="String32">
<xs:restriction base="xs:token">
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="UserId">
<xs:restriction base="xs:ID">
<xs:pattern value="\w+\.\w+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Password">
<xs:restriction base="xs:token">
<xs:maxLength value="12"/>
<xs:minLength value="6"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="EmailAddress">
<xs:restriction base="xs:token">
<xs:pattern value='[^()<>@,;:\\"
[EMAIL PROTECTED]()<>@,;:\\"[EMAIL PROTECTED]'/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="SupportedLanguage">
<xs:restriction base="xs:language">
<xs:enumeration value="en"/>
<xs:enumeration value="de"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Gender">
<xs:annotation>
<xs:documentation>
ISO 5218 Gender Codes:
0 not known
1 male
2 female
9 not specified
</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:unsignedByte">
<xs:enumeration value="0"/>
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
<xs:enumeration value="9"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Profile">
<xs:restriction base="xs:token">
<xs:enumeration value
="DBSMART_USER"></xs:enumeration>
<xs:enumeration value
="DBSMART_ADMIN"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="company.xsd"/>
<xs:include schemaLocation="smart-types.xsd"/>
<xs:annotation>
<xs:documentation xml:lang="en">
User schema for smart.railion.com.
Copyright 2003 smart.railion.com. All rights reserved.
</xs:documentation>
</xs:annotation>
<xs:complexType name="User">
<xs:sequence>
<xs:element ref="firstName"/>
<xs:element ref="lastName"/>
<xs:element ref="fullName"/>
<xs:element ref="mail"/>
<xs:element ref="phone"/>
<xs:element ref="fax" minOccurs="0"/>
<xs:element ref="gender"/>
<xs:element ref="preferredLanguage"/>
<xs:element ref="profile" minOccurs
="0" maxOccurs="unbounded"/>
<xs:element ref="company"/>
</xs:sequence>
<xs:attribute name="user_id" type="UserId" use
="required"/>
<xs:attribute name="password" type="Password" use
="optional"/>
</xs:complexType>
<xs:element name="user" type="User"/>
<xs:element name="firstName" type="String24"/>
<xs:element name="lastName" type="String24"/>
<xs:element name="fullName" type="String24"/>
<xs:element name="mail" type="EmailAddress"/>
<xs:element name="phone" type="String32"/>
<xs:element name="fax" type="String32"/>
<xs:element name="gender" type="Gender"/>
<xs:element name="preferredLanguage" type
="SupportedLanguage"/>
<xs:element name="profile" type="Profile"/>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Company">
<xs:sequence>
<xs:element ref="companyName"/>
<xs:element ref="department" minOccurs
="0"/>
<xs:element ref="street"/>
<xs:element ref="city"/>
<xs:element ref="postalCode"/>
<xs:element ref="state"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
<xs:element name="company" type="Company"/>
<xs:element name="companyName" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="postalCode" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<user user_id="peter.test" password="smartpwd">
<firstName>Peter</firstName>
<lastName>Test</lastName>
<mail>[EMAIL PROTECTED]</mail>
<phone>+49 201 8707 364</phone>
<fax>+49 201 8707 156</fax>
<gender>1</gender>
<preferredLanguage>en</preferredLanguage>
<profile>DBSMART_USER_</profile>
<company>
<companyName>Schenker AG</companyName>
<department>Systems Development</department>
<street>Alfredstr. 152</street>
<city>Essen</city>
<postalCode>45131</postalCode>
<state>NRW</state>
<country>DE</country>
</company>
</user>
---------------------------------------------------------------------
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]