diff --git a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java
index e43a984d..5e2ccc44 100644
--- a/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java
+++ b/xml/src/main/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollection.java
@@ -183,23 +183,24 @@ public class CommonsXsdSchemaCollection implements XsdSchemaCollection, Initiali
private void inlineIncludes(XmlSchema schema, Set processedIncludes, Set processedImports) {
processedIncludes.add(schema);
- XmlSchemaObjectCollection includes = schema.getIncludes();
- for (int i = 0; i < includes.getCount(); i++) {
- XmlSchemaExternal external = (XmlSchemaExternal) includes.getItem(i);
- if (external instanceof XmlSchemaInclude) {
- XmlSchema includedSchema = external.getSchema();
- XmlSchemaObjectCollection items = schema.getItems();
+
+ XmlSchemaObjectCollection schemaItems = schema.getItems();
+ for (int i = 0; i < schemaItems.getCount(); i++) {
+ XmlSchemaObject schemaObject = schemaItems.getItem(i);
+ if (schemaObject instanceof XmlSchemaInclude) {
+ XmlSchema includedSchema = ((XmlSchemaInclude) schemaObject).getSchema();
if (!processedIncludes.contains(includedSchema)) {
inlineIncludes(includedSchema, processedIncludes, processedImports);
findImports(includedSchema, processedImports, processedIncludes);
XmlSchemaObjectCollection includeItems = includedSchema.getItems();
for (int j = 0; j < includeItems.getCount(); j++) {
XmlSchemaObject includedItem = includeItems.getItem(j);
- items.add(includedItem);
+ schemaItems.add(includedItem);
}
}
// remove the
- items.remove(external);
+ schemaItems.removeAt(i);
+ i--;
}
}
}
diff --git a/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java
index c68e7d63..262c3d98 100644
--- a/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java
+++ b/xml/src/test/java/org/springframework/xml/xsd/commons/CommonsXsdSchemaCollectionTest.java
@@ -120,4 +120,29 @@ public class CommonsXsdSchemaCollectionTest extends XMLTestCase {
// expected
}
}
+
+ public void testIncludesAndImports() throws Exception {
+ Resource hr = new ClassPathResource("hr.xsd", getClass());
+ collection.setXsds(new Resource[]{hr});
+ collection.setInline(true);
+ collection.afterPropertiesSet();
+
+ XsdSchema[] schemas = collection.getXsdSchemas();
+ assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
+
+ assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas", schemas[0].getTargetNamespace());
+ Resource hr_employee = new ClassPathResource("hr_employee.xsd", getClass());
+ Document expected = documentBuilder.parse(SaxUtils.createInputSource(hr_employee));
+ DOMResult domResult = new DOMResult();
+ transformer.transform(schemas[0].getSource(), domResult);
+ assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
+
+ assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas/holiday", schemas[1].getTargetNamespace());
+ Resource holiday = new ClassPathResource("holiday.xsd", getClass());
+ expected = documentBuilder.parse(SaxUtils.createInputSource(holiday));
+ domResult = new DOMResult();
+ transformer.transform(schemas[1].getSource(), domResult);
+ assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
+
+ }
}
\ No newline at end of file
diff --git a/xml/src/test/resources/org/springframework/xml/xsd/commons/employee.xsd b/xml/src/test/resources/org/springframework/xml/xsd/commons/employee.xsd
new file mode 100644
index 00000000..b7e91690
--- /dev/null
+++ b/xml/src/test/resources/org/springframework/xml/xsd/commons/employee.xsd
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/xml/src/test/resources/org/springframework/xml/xsd/commons/holiday.xsd b/xml/src/test/resources/org/springframework/xml/xsd/commons/holiday.xsd
new file mode 100644
index 00000000..3f16a51a
--- /dev/null
+++ b/xml/src/test/resources/org/springframework/xml/xsd/commons/holiday.xsd
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/xml/src/test/resources/org/springframework/xml/xsd/commons/hr.xsd b/xml/src/test/resources/org/springframework/xml/xsd/commons/hr.xsd
new file mode 100644
index 00000000..fa13389a
--- /dev/null
+++ b/xml/src/test/resources/org/springframework/xml/xsd/commons/hr.xsd
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/xml/src/test/resources/org/springframework/xml/xsd/commons/hr_employee.xsd b/xml/src/test/resources/org/springframework/xml/xsd/commons/hr_employee.xsd
new file mode 100644
index 00000000..4fb3e0a6
--- /dev/null
+++ b/xml/src/test/resources/org/springframework/xml/xsd/commons/hr_employee.xsd
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file