[XMLSCHEMA-20] Fix problem of serialized import elements always having
a targetNamespace attribute that may be empty.
diff --git a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
index c5e839c..b11afd4 100644
--- a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
+++ b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaSerializer.java
@@ -1392,7 +1392,7 @@
Element importEl = createNewElement(doc, "import",
schema.getSchemaNamespacePrefix(), XmlSchema.SCHEMA_NS);
- if (importObj.namespace != null) {
+ if (importObj.namespace != null && !"".equals(importObj.namespace)) {
importEl.setAttributeNS(null, "namespace", importObj.namespace);
}
@@ -1555,10 +1555,13 @@
schemaElement = serializedSchema;
if (schemaObj.getSyntacticalTargetNamespace() != null) {
- serializedSchema.setAttributeNS(null, "targetNamespace", schemaObj.getSyntacticalTargetNamespace());
+ String targetNamespace = schemaObj.getSyntacticalTargetNamespace();
+ if (targetNamespace != null && !"".equals(targetNamespace)) {
+ serializedSchema.setAttributeNS(null, "targetNamespace", targetNamespace);
+ }
String targetNS =
- (String)schemaNamespace.get(schemaObj.getSyntacticalTargetNamespace());
+ (String)schemaNamespace.get(targetNamespace);
//if the namespace is not entered then add
//the targetNamespace
diff --git a/xmlschema-core/src/test/java/tests/IncludeTest.java b/xmlschema-core/src/test/java/tests/IncludeTest.java
index 9a8751a..46cf579 100644
--- a/xmlschema-core/src/test/java/tests/IncludeTest.java
+++ b/xmlschema-core/src/test/java/tests/IncludeTest.java
@@ -28,6 +28,9 @@
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
import org.xml.sax.InputSource;
import org.apache.ws.commons.schema.XmlSchema;
@@ -35,6 +38,7 @@
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaExternal;
import org.apache.ws.commons.schema.XmlSchemaInclude;
+import org.apache.ws.commons.schema.constants.Constants;
import org.junit.Assert;
import org.junit.Test;
@@ -161,4 +165,21 @@
XmlSchema schema = schemaCol.read(isource);
assertNotNull(schema);
}
+
+
+
+ @Test
+ public void testSerializeSchema() throws Exception {
+ String uri = Resources.asURI("XMLSCHEMA-20/test.xsd");
+ InputSource isource = new InputSource(new FileInputStream(uri));
+ isource.setSystemId(uri);
+ XmlSchemaCollection schemaCol = new XmlSchemaCollection();
+ XmlSchema schema = schemaCol.read(isource);
+ assertNotNull(schema);
+ Document doc = schema.getSchemaDocument();
+ Element el = (Element)doc.getDocumentElement()
+ .getElementsByTagNameNS(Constants.URI_2001_SCHEMA_XSD, "import").item(0);
+ assertNotNull(el);
+ assertNull(el.getAttributeNodeNS(null, "targetNamespace"));
+ }
}
diff --git a/xmlschema-core/src/test/resources/XMLSCHEMA-20/test.xsd b/xmlschema-core/src/test/resources/XMLSCHEMA-20/test.xsd
new file mode 100644
index 0000000..7101384
--- /dev/null
+++ b/xmlschema-core/src/test/resources/XMLSCHEMA-20/test.xsd
@@ -0,0 +1,15 @@
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ targetNamespace="http://example.org/ord"
+ xmlns="http://example.org/ord">
+
+ <xs:include schemaLocation="test2.xsd" />
+ <xs:import schemaLocation="test3.xsd" />
+
+ <xs:element name="order" type="OrderType" />
+ <xs:complexType name="OrderType">
+ <xs:sequence>
+ <xs:element name="number" type="OrderNumType" />
+ </xs:sequence>
+ </xs:complexType>
+
+</xs:schema>
\ No newline at end of file
diff --git a/xmlschema-core/src/test/resources/XMLSCHEMA-20/test2.xsd b/xmlschema-core/src/test/resources/XMLSCHEMA-20/test2.xsd
new file mode 100644
index 0000000..88a6691
--- /dev/null
+++ b/xmlschema-core/src/test/resources/XMLSCHEMA-20/test2.xsd
@@ -0,0 +1,9 @@
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ xmlns="http://example.org/ord"
+ targetNamespace="http://example.org/ord">
+
+ <xs:simpleType name="OrderNumType">
+ <xs:restriction base="xs:string" />
+ </xs:simpleType>
+
+</xs:schema>
\ No newline at end of file
diff --git a/xmlschema-core/src/test/resources/XMLSCHEMA-20/test3.xsd b/xmlschema-core/src/test/resources/XMLSCHEMA-20/test3.xsd
new file mode 100644
index 0000000..d58674a
--- /dev/null
+++ b/xmlschema-core/src/test/resources/XMLSCHEMA-20/test3.xsd
@@ -0,0 +1,2 @@
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+</xs:schema>
\ No newline at end of file