}
try {
DynamicResourceIterator resourceList = new DynamicResourceIterator();
resourceList.addAll(Arrays.asList(sources));
while(resourceList.hasNext()) {
ResourceInputStream myStream = resourceList.nextResource();
Document doc = builder.parse(myStream);
NodeList nodeList = (NodeList) xPath.evaluate(IMPORT_PATH, doc, XPathConstants.NODESET);
int length = nodeList.getLength();
for (int j=0;j<length;j++) {
Element element = (Element) nodeList.item(j);
Resource resource = loader.getResource(element.getAttribute("resource"));
ResourceInputStream ris = new ResourceInputStream(resource.getInputStream(), resource.getURL().toString());
resourceList.addEmbeddedResource(ris);
element.getParentNode().removeChild(element);
}
if (length > 0) {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer xmlTransformer = tFactory.newTransformer();
xmlTransformer.setOutputProperty(OutputKeys.VERSION, "1.0");
xmlTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
xmlTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos));
StreamResult result = new StreamResult(writer);
xmlTransformer.transform(source, result);
byte[] itemArray = baos.toByteArray();
resourceList.set(resourceList.getPosition() - 1, new ResourceInputStream(new ByteArrayInputStream(itemArray), null, myStream.getNames()));
} else {
myStream.reset();
}
}