When using org.exolab.castor.xml.schema.writer.SchemaWriter to output a
schema that was read in, I noticed that it does not properly add the
namespace qualification on the base attribute of the <xsd:extension>. This
problem is in castor-0.9.5, castor-0.9.4.1, and possibly other earlier
versions.
Example:
Original - <xsd:extension base="qb:RequestT">
With SchemaWriter - <xsd:extension base="RequestT">
Notice how the qb: prefix is getting stripped.
Problem:
In SchemaWriter.processComplexType() the namespace for the BaseType is
mis-calculated on line 581. This will always result in baseType always being
outputted with the same name space as complexType, which is not always the
case.
if (baseType.isComplexType()) {
String targetNamespace =
complexType.getSchema().getTargetNamespace();
String nsPrefix = getNSPrefix(complexType.getSchema(),
targetNamespace);
if ((nsPrefix != null) && (nsPrefix.length() != 0))
baseTypeName = nsPrefix +':'+ baseTypeName;
targetNamespace = null;
nsPrefix = null;
}
Fix:
Since the baseType can come from a different schema, we need to get the
target namespace from the baseType and not the type that extends it.
if (baseType.isComplexType()) {
// The line below was modified
//String targetNamespace =
complexType.getSchema().getTargetNamespace();
String targetNamespace =
baseType.getSchema().getTargetNamespace();
String nsPrefix = getNSPrefix(complexType.getSchema(),
targetNamespace);
if ((nsPrefix != null) && (nsPrefix.length() != 0))
baseTypeName = nsPrefix +':'+ baseTypeName;
targetNamespace = null;
nsPrefix = null;
}
Reproduce the Problem:
Have a file bar.xsd like:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema targetNamespace="http://www.foo.com/XMLSchema/NS"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.foo.com/XMLSchema/NS" elementFormDefault="qualified">
<xsd:complexType name="RequestT">
<xsd:sequence>
<xsd:element name="RequestId" type="xsd:nonNegativeInteger" />
<xsd:element name="TargetSchemaVersionUsed" minOccurs="0"
type="xsd:nonNegativeInteger"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
And have a file foo.xsd like:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified"
xmlns:qb="http://www.foo.com/XMLSchema/NS" >
<xsd:import namespace="http://www.foo.com/XMLSchema/NS"
schemaLocation="bar.xsd"/>
<xsd:complexType name="Foo">
<xsd:complexContent>
<xsd:extension base="qb:RequestT">
<xsd:sequence>
<xsd:element name="Baz"
type="xsd:nonNegativeInteger"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
Read in foo.xsd with a SchemaReader and then output it with a SchemaWriter
and notice the namespace for the base attribute of the xsd:extesion tag.
Something like the following should work:
SchemaReader r = new SchemaReader("c:/temp/foo.xsd");
Schema s = r.read();
StringWriter stw = new StringWriter();
SchemaWriter sw = new SchemaWriter(stw);
sw.write(s);
System.out.println("Foo.xsd after processing: " + stw.toString());
-Jeff
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev