Index: tmp/org.txm.core/src/java/org/txm/xml/XMLParser.java
===================================================================
--- tmp/org.txm.core/src/java/org/txm/xml/XMLParser.java	(revision 2938)
+++ tmp/org.txm.core/src/java/org/txm/xml/XMLParser.java	(revision 2939)
@@ -45,7 +45,7 @@
 	
 	public static String TEI = "tei";
 	
-	protected static PersonalNamespaceContext Nscontext = new PersonalNamespaceContext();
+	protected PersonalNamespaceContext Nscontext = new PersonalNamespaceContext();
 	
 	protected StringBuilder currentXPath = new StringBuilder("");
 	
@@ -91,7 +91,14 @@
 		
 	}
 	
-	protected void after() throws XMLStreamException, IOException {
+	/**
+	 * 
+	 * @param allWentWell true if the process ends well
+	 * 
+	 * @throws XMLStreamException
+	 * @throws IOException
+	 */
+	protected void after(boolean allWentWell) throws XMLStreamException, IOException {
 		if (factory != null) {
 			factory = null;
 			
@@ -120,23 +127,30 @@
 					System.out.println("inputData excep: " + e);
 				}
 			}
-			
-			
 		}
 	}
 	
 	public final static String SLASH = "/";
 	
+	/**
+	 * true if the first start element was parsed
+	 */
+	protected boolean firstElementParsed = false;
+	
 	public final boolean process() throws XMLStreamException, IOException {
 		
 		// if (processingXInclude == 0) {
 		before(); // if you need to do something before reading the xml
 		// }
+		
+		firstElementParsed = false;
+		
 		try {
-			for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
+			for (int event = parser.getEventType(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
 				
 				// preparing process
 				if (event == XMLStreamConstants.START_ELEMENT) {
+					firstElementParsed = true;
 					localname = parser.getLocalName();
 					currentXPath.append(SLASH);
 					currentXPath.append(localname);
@@ -185,10 +199,11 @@
 			System.out.println("Location line: " + parser.getLocation().getLineNumber() + " character: " + parser.getLocation().getColumnNumber());
 			org.txm.utils.logger.Log.printStackTrace(e);
 			// e.printStackTrace();
+			after(false); // if you need to do something before closing the parser();
 			return false;
 		}
 		finally {
-			after(); // if you need to do something before closing the parser();
+			after(true); // if you need to do something before closing the parser();
 		}
 		
 		return true;
@@ -196,6 +211,15 @@
 	
 	protected final void processParserEvent(int event) throws XMLStreamException, IOException {
 		switch (event) {
+			case XMLStreamConstants.START_DOCUMENT:
+				processStartDocument();
+			break;
+			case XMLStreamConstants.SPACE:
+				processSpace();
+			break;
+			case XMLStreamConstants.NOTATION_DECLARATION:
+				processNotationDeclaration();
+			break;
 			case XMLStreamConstants.NAMESPACE:
 				processNamespace();
 				break;
@@ -229,65 +253,6 @@
 		}
 	}
 	
-	/**
-	 * The start element has already been written
-	 * 
-	 * @param tagname
-	 * @throws XMLStreamException
-	 * @throws IOException
-	 */
-	public void goToEnd(String tagname) throws XMLStreamException, IOException {
-		int elements = 1;
-		for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
-			// System.out.println("event "+event
-			switch (event) {
-				case XMLStreamConstants.NAMESPACE:
-					processNamespace();
-					break;
-				case XMLStreamConstants.START_ELEMENT:
-					elements++;
-					localname = parser.getLocalName();
-					currentXPath.append(SLASH);
-					currentXPath.append(localname);
-					buildCurrentAttributes();
-					
-					processStartElement();
-					break;
-				case XMLStreamConstants.CHARACTERS:
-					processCharacters();
-					break;
-				case XMLStreamConstants.PROCESSING_INSTRUCTION:
-					processProcessingInstruction();
-					break;
-				case XMLStreamConstants.DTD:
-					processDTD();
-					break;
-				case XMLStreamConstants.CDATA:
-					processCDATA();
-					break;
-				case XMLStreamConstants.COMMENT:
-					processComment();
-					break;
-				case XMLStreamConstants.END_ELEMENT:
-					elements--;
-					localname = parser.getLocalName();
-					
-					currentXPath.setLength(currentXPath.length() - localname.length() - 1);
-					
-					if (elements == 0 && localname == tagname)
-						return;
-					break;
-				case XMLStreamConstants.END_DOCUMENT:
-					processEndDocument();
-					break;
-				case XMLStreamConstants.ENTITY_REFERENCE:
-					processEntityReference();
-					break;
-			}
-		}
-		
-	}
-	
 	public final String getLocation() {
 		if (parser != null) {
 			return "Line: " + parser.getLocation().getLineNumber() + " Col: " + parser.getLocation().getColumnNumber();
@@ -313,6 +278,12 @@
 		return attributesStack.lastElement();
 	}
 	
+	protected void processStartDocument() throws XMLStreamException {}
+	
+	protected void processSpace() throws XMLStreamException {}
+	
+	protected void processNotationDeclaration() throws XMLStreamException {}
+	
 	protected void processNamespace() throws XMLStreamException {}
 	
 	protected void processStartElement() throws XMLStreamException, IOException {}
Index: tmp/org.txm.core/src/java/org/txm/xml/XMLProcessor.java
===================================================================
--- tmp/org.txm.core/src/java/org/txm/xml/XMLProcessor.java	(revision 2938)
+++ tmp/org.txm.core/src/java/org/txm/xml/XMLProcessor.java	(revision 2939)
@@ -2,10 +2,10 @@
 
 import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.URL;
-import java.util.Arrays;
 import java.util.LinkedHashMap;
 
 import javax.xml.stream.XMLOutputFactory;
@@ -14,8 +14,10 @@
 import javax.xml.stream.XMLStreamWriter;
 
 /**
- * Parse and write a XML file. If no hook specified the file is not transformed
+ * Parse and write a XML file. If no hook specified the file is not transformed (mimic the implementation of the old StaxIdentityParser)
  * 
+ * See thee available Hook&Activators classes to do more things
+ * 
  * @author mdecorde
  *
  */
@@ -28,16 +30,28 @@
 	
 	protected XMLStreamWriter writer;
 	
+	/**
+	 * 
+	 * @param infile the file to read
+	 * @throws IOException
+	 * @throws XMLStreamException
+	 */
 	public XMLProcessor(File infile) throws IOException, XMLStreamException {
 		this(infile.toURI().toURL());
 	}
 	
+	/**
+	 * 
+	 * @param inputurl the input URL to read
+	 * @throws IOException
+	 * @throws XMLStreamException
+	 */
 	public XMLProcessor(URL inputurl) throws IOException, XMLStreamException {
 		super(inputurl);
 	}
 	
 	/**
-	 * Helper method to get an attribute value by its name
+	 * Helper method to get a Stax element attribute value using its name
 	 * 
 	 * @param name the attribute name
 	 * @return the value if any
@@ -46,6 +60,12 @@
 	public String getParserAttributeValue(String name) {
 		if (name == null) return null;
 		
+		// try the no namespace attribute first
+		if (parser.getAttributeValue(null, name) != null) {
+			return parser.getAttributeValue(null, name);
+		}
+		
+		//no luck try all attributes 
 		int c = parser.getAttributeCount();
 		for (int i = 0; i < c; i++) {
 			if (name.equals(parser.getAttributeLocalName(i))) {
@@ -62,27 +82,14 @@
 		super.before();
 	}
 	
+	/**
+	 * closing parser and writer
+	 */
 	@Override
-	protected void after() throws XMLStreamException, IOException {
+	protected void after(boolean allWentWell) throws XMLStreamException, IOException {
 		
-		super.after();
-		
-		if (factory != null) {
-			factory = null;
-			if (parser != null) {
-				parser.close();
-			}
-			writer.flush();
-			if (writer != null) {
-				writer.close();
-			}
-			if (inputData != null) {
-				inputData.close();
-			}
-			writer = null;
-			parser = null;
-		}
-		
+		super.after(allWentWell); // close parser, inputData and factory
+				
 		if (writer != null) writer.close();
 		
 		if (output != null) output.close();
@@ -93,9 +100,10 @@
 	 *
 	 * @param outfile the outfile
 	 * @return true, if successful
+	 * @throws XMLStreamException 
+	 * @throws IOException 
 	 */
-	private boolean createOutput(File f) {
-		try {
+	private boolean createOutput(File f) throws XMLStreamException, IOException {
 			if (writer != null) { // process from a file
 				writer.close();
 			}
@@ -108,11 +116,6 @@
 			writer = outfactory.createXMLStreamWriter(output, "UTF-8");// create a new file
 			writer.setNamespaceContext(Nscontext);
 			return true;
-		}
-		catch (Exception e) {
-			System.out.println("Error: create output of " + f + ": " + e);
-			return false;
-		}
 	}
 	
 	public boolean process(File outFile) throws XMLStreamException, IOException {
@@ -121,8 +124,6 @@
 			return false;
 		}
 		
-		writer.writeStartDocument("UTF-8", "1.0");
-		writer.writeCharacters("\n");
 		
 		boolean ret = process(writer);
 		
@@ -145,19 +146,28 @@
 	/**
 	 * Go to the end of an element :
 	 * - The start element has already been written
-	 * - the content is skipped and not written
+	 * - the content is skipped and written
 	 * 
+	 * @return true if all went well
 	 * @param tagname
 	 * @throws XMLStreamException
 	 * @throws IOException
 	 */
-	@Override
-	public void goToEnd(String tagname) throws XMLStreamException, IOException {
+	public boolean goToEnd(String tagname) throws XMLStreamException, IOException {
 		int elements = 1;
 		try {
 			for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
 				// System.out.println("event "+event
 				switch (event) {
+					case XMLStreamConstants.START_DOCUMENT:
+						processStartDocument();
+					break;
+					case XMLStreamConstants.SPACE:
+						processSpace();
+					break;
+					case XMLStreamConstants.NOTATION_DECLARATION:
+						processNotationDeclaration();
+					break;
 					case XMLStreamConstants.NAMESPACE:
 						processNamespace();
 						break;
@@ -193,8 +203,9 @@
 						
 						currentXPath.setLength(currentXPath.length() - localname.length() - 1);
 						
-						if (elements == 0 && localname == tagname)
-							return;
+						if (elements == 0 && localname == tagname) {
+							return false;
+						}
 						break;
 					case XMLStreamConstants.END_DOCUMENT:
 						processEndDocument();
@@ -209,19 +220,26 @@
 			System.out.println("Error while parsing file " + inputurl);
 			System.out.println("Location " + parser.getLocation());
 			org.txm.utils.logger.Log.printStackTrace(e);
-			after();
-			return;
+			after(false);
+			return false;
 		}
+		return true;
 	}
 	
+	protected void processStartDocument() throws XMLStreamException {
+		writer.writeStartDocument(parser.getCharacterEncodingScheme(), parser.getVersion());
+		writer.writeCharacters("\n");
+	}
+
 	@Override
 	protected void processNamespace() throws XMLStreamException {
+		this.Nscontext.addNamespace(parser.getPrefix(), parser.getNamespaceURI());
 		writer.writeNamespace(parser.getPrefix(), parser.getNamespaceURI());
 	}
 	
-	
 	@Override
 	protected void processStartElement() throws XMLStreamException, IOException {
+		
 		String prefix = parser.getPrefix();
 		if (prefix != null && prefix.length() > 0) {
 			writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname);
@@ -260,6 +278,7 @@
 	@Override
 	protected void processProcessingInstruction() throws XMLStreamException {
 		writer.writeProcessingInstruction(parser.getPITarget(), parser.getPIData());
+		if (!firstElementParsed) writer.writeCharacters("\n");
 	}
 	
 	@Override
@@ -299,8 +318,8 @@
 	
 	public static void main(String[] args) {
 		try {
-			File input = new File(System.getProperty("user.home"), "xml/tdm80j/tdm80j.xml");
-			File output = new File(System.getProperty("user.home"), "xml/tdm80j/out.xml");
+			File input = new File(System.getProperty("user.home"), "xml/identity/test.xml");
+			File output = new File(System.getProperty("user.home"), "xml/identity/test-copy.xml");
 			if (!(input.exists() && input.canRead())) {
 				System.out.println("cannot found $input");
 				return;
@@ -329,37 +348,37 @@
 			// parentParser.writer.writeAttribute("hook", name);
 			// }
 			// };
-			XPathHookActivator xpathActivator = new XPathHookActivator(null, "//p");
-			XPathsHookActivator xpathsActivator = new XPathsHookActivator(null, Arrays.asList("//p", "//p"));
-			IdentityHook hook2 = new IdentityHook("hook2", xpathsActivator, builder) {
-				
-				@Override
-				public boolean _activate() {
-					System.out.println("ACTIVATING " + name + " AT " + getLocation());
-					return true;
-				}
-				
-				@Override
-				public boolean deactivate() {
-					System.out.println("DEACTIVATING " + name + " AT " + getLocation());
-					return true;
-				}
-				
-				@Override
-				public void processStartElement() throws XMLStreamException, IOException {
-					super.processStartElement();
-					parentParser.writer.writeAttribute("hook", name);
-				}
-			};
-			
-			// DOMIdentityHook hook3 = new DOMIdentityHook("hook3", xpathsActivator, builder) {
+//			XPathHookActivator xpathActivator = new XPathHookActivator(null, "//p");
+//			XPathsHookActivator xpathsActivator = new XPathsHookActivator(null, Arrays.asList("//p", "//p"));
+//			IdentityHook hook2 = new IdentityHook("hook2", xpathsActivator, builder) {
+//				
+//				@Override
+//				public boolean _activate() {
+//					System.out.println("ACTIVATING " + name + " AT " + getLocation());
+//					return true;
+//				}
+//				
+//				@Override
+//				public boolean deactivate() {
+//					System.out.println("DEACTIVATING " + name + " AT " + getLocation());
+//					return true;
+//				}
+//				
+//				@Override
+//				public void processStartElement() throws XMLStreamException, IOException {
+//					super.processStartElement();
+//					parentParser.writer.writeAttribute("hook", name);
+//				}
+//			};
+//			
+//			// DOMIdentityHook hook3 = new DOMIdentityHook("hook3", xpathsActivator, builder) {
 			//
 			// };
 			
 			long time = System.currentTimeMillis();
 			if (builder.process(output)) {
 				System.out.println("Time=" + (System.currentTimeMillis() - time));
-				System.out.println("XPaths activator has done working ? " + xpathsActivator.hasAllXpathsBeenProcessed());
+				//System.out.println("XPaths activator has done working ? " + xpathsActivator.hasAllXpathsBeenProcessed());
 			}
 			else {
 				System.out.println("failure !");
Index: tmp/org.txm.core/src/java/org/txm/importer/PersonalNamespaceContext.java
===================================================================
--- tmp/org.txm.core/src/java/org/txm/importer/PersonalNamespaceContext.java	(revision 2938)
+++ tmp/org.txm.core/src/java/org/txm/importer/PersonalNamespaceContext.java	(revision 2939)
@@ -44,17 +44,17 @@
 	
 	public static HashMap<String, String> adress2name = new HashMap<String, String>();
 	public static HashMap<String, String> name2adress = new HashMap<String, String>();
-	static {
-		addNamespace("tei", "http://www.tei-c.org/ns/1.0");
-		addNamespace("me", "http://www.menota.org/ns/1.0");
-		addNamespace("bfm", "http://bfm.ens-lsh.fr/ns/1.0");
-		addNamespace("txm", "http://textometrie.org/1.0");
-		addNamespace("xi", "http://www.w3.org/2001/XInclude");
-		addNamespace("fn", "http://www.w3.org/2005/xpath-functions");
-		addNamespace("xhtml", "http://www.w3.org/1999/xhtml");
-		addNamespace("svg", "http://www.w3.org/2000/svg");
-		addNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
-	}
+//	static {
+//		addNamespace("tei", "http://www.tei-c.org/ns/1.0");
+//		addNamespace("me", "http://www.menota.org/ns/1.0");
+//		addNamespace("bfm", "http://bfm.ens-lsh.fr/ns/1.0");
+//		addNamespace("txm", "http://textometrie.org/1.0");
+//		addNamespace("xi", "http://www.w3.org/2001/XInclude");
+//		addNamespace("fn", "http://www.w3.org/2005/xpath-functions");
+//		addNamespace("xhtml", "http://www.w3.org/1999/xhtml");
+//		addNamespace("svg", "http://www.w3.org/2000/svg");
+//		addNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
+//	}
 	/*
 	String TEINS = "http://www.tei-c.org/ns/1.0"
 	String MENS = "http://www.menota.org/ns/1.0";
@@ -79,7 +79,7 @@
 		super();
 	}
 
-	public static void addNamespace(String name, String address) {
+	public void addNamespace(String name, String address) {
 		adress2name.put(address, name);
 		name2adress.put(name, address);
 	}
Index: tmp/org.txm.core/src/java/org/txm/importer/StaxIdentityParser.java
===================================================================
--- tmp/org.txm.core/src/java/org/txm/importer/StaxIdentityParser.java	(revision 2938)
+++ tmp/org.txm.core/src/java/org/txm/importer/StaxIdentityParser.java	(revision 2939)
@@ -14,50 +14,57 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 
+/**
+ * XML Identity transformation. extends the processXYZ methods to do something
+ * @deprecated use the  org.txm.xml.XMLProcessor instead. The XMLProcessor implements Hooks to manage XML Event or XML DOM while parsing
+ * @author mdecorde
+ *
+ */
 public class StaxIdentityParser {
-	
+
 	/** The input */
 	protected URL inputurl;
-	
+
 	protected InputStream inputData;
-	
+
 	protected XMLInputFactory factory;
-	
+
 	protected XMLStreamReader parser;
-	
+
 	/** The output. */
 	protected XMLOutputFactory outfactory = XMLOutputFactory.newInstance();
-	
+
 	protected BufferedOutputStream output;
-	
+
 	protected XMLStreamWriter writer;
-	
+
 	public static String TXMNS = "http://textometrie.org/1.0";
-	
+
 	public static String TXM = "txm";
-	
+
 	public static String TEINS = "http://www.tei-c.org/ns/1.0";
-	
+
 	public static String TEI = "tei";
-	
-	protected static PersonalNamespaceContext Nscontext = new PersonalNamespaceContext();
-	
+
+	protected PersonalNamespaceContext Nscontext = new PersonalNamespaceContext();
+
 	// protected StringBuilder currentXPath = new StringBuilder("/")
 	protected String localname;
-	
+
 	int processingXInclude = 0;
-	
+
 	public StaxIdentityParser(File infile) throws IOException, XMLStreamException {
 		this(infile.toURI().toURL());
 	}
-	
+
 	public StaxIdentityParser(URL inputurl) throws IOException, XMLStreamException {
 		this.inputurl = inputurl;
 		this.inputData = inputurl.openStream();
 		this.factory = XMLInputFactory.newInstance();
 		this.parser = factory.createXMLStreamReader(inputData);
+		
 	}
-	
+
 	/**
 	 * Helper method to get an attribute value
 	 * 
@@ -66,21 +73,21 @@
 	 */
 	public String getParserAttributeValue(String name) {
 		if (name == null) return null;
-		
+
 		int c = parser.getAttributeCount();
 		for (int i = 0; i < c; i++) {
 			if (name.equals(parser.getAttributeLocalName(i))) {
 				return parser.getAttributeValue(i);
 			}
 		}
-		
+
 		return null;
 	}
-	
+
 	protected void before() {
-		
+
 	}
-	
+
 	protected void after() throws XMLStreamException, IOException {
 		factory = null;
 		if (parser != null) parser.close();
@@ -90,12 +97,12 @@
 		writer = null;
 		parser = null;
 	}
-	
+
 	protected void closeForError() throws XMLStreamException, IOException {
 		if (parser != null) parser.close();
 		if (inputData != null) inputData.close();
 	}
-	
+
 	/**
 	 * Creates the output.
 	 *
@@ -108,9 +115,9 @@
 				writer.close();
 			if (output != null) // process from a file
 				output.close();
-			
+
 			output = new BufferedOutputStream(new FileOutputStream(f), 16 * 1024);
-			
+
 			writer = outfactory.createXMLStreamWriter(output, "UTF-8");// create a new file
 			writer.setNamespaceContext(Nscontext);
 			return true;
@@ -120,13 +127,14 @@
 			return false;
 		}
 	}
-	
+
 	public boolean process(File outfile) throws XMLStreamException, IOException {
 		if (!createOutput(outfile))
 			return false;
-		
-		writer.writeStartDocument("UTF-8", "1.0");
-		writer.writeCharacters("\n");
+
+//		//writer.writeStartDocument("UTF-8", "1.0");
+//		writer.writeStartDocument();
+//		writer.writeCharacters("\n");
 		boolean ret = process(writer);
 		if (writer != null) {
 			writer.close();
@@ -139,7 +147,7 @@
 				System.out.println("output excep: " + e);
 			}
 		}
-		
+
 		if (parser != null) {
 			try {
 				parser.close();
@@ -148,7 +156,7 @@
 				System.out.println("parser excep: " + e);
 			}
 		}
-		
+
 		if (inputData != null) {
 			try {
 				inputData.close();
@@ -157,54 +165,66 @@
 				System.out.println("inputData excep: " + e);
 			}
 		}
-		
+
 		return ret;
 	}
-	
+
 	public final static String SLASH = "/";
-	
+	public boolean firstElementStarted = false;
 	public boolean process(XMLStreamWriter awriter) throws XMLStreamException, IOException {
+		
+		firstElementStarted = false;
 		this.writer = awriter;
 		// if (processingXInclude == 0) {
-		before(); // if you need to do something before reading the xml
+		before(); // if you need to do something before parsing the XML
 		// }
 		try {
-			for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
+			for (int event = parser.getEventType(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
 				switch (event) {
-					case XMLStreamConstants.NAMESPACE:
-						processNamespace();
-						break;
-					case XMLStreamConstants.START_ELEMENT:
-						localname = parser.getLocalName();
-						// currentXPath.append(SLASH)
-						processStartElement();
-						break;
-					case XMLStreamConstants.CHARACTERS:
-						processCharacters();
-						break;
-					case XMLStreamConstants.END_ELEMENT:
-						localname = parser.getLocalName();
-						processEndElement();
-						// currentXPath.substring(0, currentXPath.length() - localname.length() -1)
-						break;
-					case XMLStreamConstants.PROCESSING_INSTRUCTION:
-						processProcessingInstruction();
-						break;
-					case XMLStreamConstants.DTD:
-						processDTD();
-						break;
-					case XMLStreamConstants.CDATA:
-						processCDATA();
-						break;
-					case XMLStreamConstants.COMMENT:
-						processComment();
-						break;
-					case XMLStreamConstants.END_DOCUMENT:
-						processEndDocument();
-						break;
-					case XMLStreamConstants.ENTITY_REFERENCE:
-						processEntityReference();
-						break;
+				case XMLStreamConstants.START_DOCUMENT:
+					writer.writeStartDocument(parser.getCharacterEncodingScheme(), parser.getVersion());
+					writer.writeCharacters("\n");
+					break;
+				case XMLStreamConstants.NAMESPACE:
+					this.Nscontext.addNamespace(parser.getPrefix(), parser.getNamespaceURI());
+					processNamespace();
+					break;
+				case XMLStreamConstants.START_ELEMENT:
+					firstElementStarted = true;
+					localname = parser.getLocalName();
+					// currentXPath.append(SLASH)
+					processStartElement();
+					break;
+				case XMLStreamConstants.NOTATION_DECLARATION:
+					break;
+				case XMLStreamConstants.SPACE:
+					break;
+				case XMLStreamConstants.CHARACTERS:
+					processCharacters();
+					break;
+				case XMLStreamConstants.END_ELEMENT:
+					localname = parser.getLocalName();
+					processEndElement();
+					// currentXPath.substring(0, currentXPath.length() - localname.length() -1)
+					break;
+				case XMLStreamConstants.PROCESSING_INSTRUCTION:
+					processProcessingInstruction();
+					break;
+				case XMLStreamConstants.DTD:
+					processDTD();
+					break;
+				case XMLStreamConstants.CDATA:
+					processCDATA();
+					break;
+				case XMLStreamConstants.COMMENT:
+					processComment();
+					break;
+				case XMLStreamConstants.END_DOCUMENT:
+					processEndDocument();
+					break;
+				case XMLStreamConstants.ENTITY_REFERENCE:
+					processEntityReference();
+					break;
 				}
 			}
 		}
@@ -224,7 +244,7 @@
 		// }
 		return true;
 	}
-	
+
 	/**
 	 * The start element has already been written
 	 * 
@@ -239,44 +259,44 @@
 			for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
 				// System.out.println("event "+event
 				switch (event) {
-					case XMLStreamConstants.NAMESPACE:
-						processNamespace();
-						break;
-					case XMLStreamConstants.START_ELEMENT:
-						elements++;
-						localname = parser.getLocalName();
-						// currentXPath.append(SLASH)
-						_processStartElement();
-						break;
-					case XMLStreamConstants.CHARACTERS:
-						processCharacters();
-						break;
-					case XMLStreamConstants.PROCESSING_INSTRUCTION:
-						processProcessingInstruction();
-						break;
-					case XMLStreamConstants.DTD:
-						processDTD();
-						break;
-					case XMLStreamConstants.CDATA:
-						processCDATA();
-						break;
-					case XMLStreamConstants.COMMENT:
-						processComment();
-						break;
-					case XMLStreamConstants.END_ELEMENT:
-						elements--;
-						localname = parser.getLocalName();
-						// currentXPath.substring(0, currentXPath.length() - localname.length() -1)
-						writer.writeEndElement();
-						if (elements == 0 && localname == tagname)
-							return;
-						break;
-					case XMLStreamConstants.END_DOCUMENT:
-						processEndDocument();
-						break;
-					case XMLStreamConstants.ENTITY_REFERENCE:
-						processEntityReference();
-						break;
+				case XMLStreamConstants.NAMESPACE:
+					processNamespace();
+					break;
+				case XMLStreamConstants.START_ELEMENT:
+					elements++;
+					localname = parser.getLocalName();
+					// currentXPath.append(SLASH)
+					_processStartElement();
+					break;
+				case XMLStreamConstants.CHARACTERS:
+					processCharacters();
+					break;
+				case XMLStreamConstants.PROCESSING_INSTRUCTION:
+					processProcessingInstruction();
+					break;
+				case XMLStreamConstants.DTD:
+					processDTD();
+					break;
+				case XMLStreamConstants.CDATA:
+					processCDATA();
+					break;
+				case XMLStreamConstants.COMMENT:
+					processComment();
+					break;
+				case XMLStreamConstants.END_ELEMENT:
+					elements--;
+					localname = parser.getLocalName();
+					// currentXPath.substring(0, currentXPath.length() - localname.length() -1)
+					writer.writeEndElement();
+					if (elements == 0 && localname == tagname)
+						return;
+					break;
+				case XMLStreamConstants.END_DOCUMENT:
+					processEndDocument();
+					break;
+				case XMLStreamConstants.ENTITY_REFERENCE:
+					processEntityReference();
+					break;
 				}
 			}
 		}
@@ -289,21 +309,21 @@
 			return;
 		}
 	}
-	
+
 	public String getLocation() {
 		if (parser != null)
 			return "Line: " + parser.getLocation().getLineNumber() + " Col: " + parser.getLocation().getColumnNumber();
 		return null;
 	}
-	
+
 	protected void processNamespace() throws XMLStreamException {
 		writer.writeNamespace(parser.getPrefix(), parser.getNamespaceURI());
 	}
-	
+
 	public static final String INCLUDE = "include";
-	
+
 	public static final String XI = "xi";
-	
+
 	protected void processStartElement() throws XMLStreamException, IOException {
 		String prefix = parser.getPrefix();
 		if (INCLUDE == localname && XI == prefix) {
@@ -314,15 +334,15 @@
 				writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname);
 			else
 				writer.writeStartElement(localname);
-			
+
 			for (int i = 0; i < parser.getNamespaceCount(); i++) {
 				writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
 			}
-			
+
 			writeAttributes();
 		}
 	}
-	
+
 	private void _processStartElement() throws XMLStreamException, IOException {
 		String prefix = parser.getPrefix();
 		if (INCLUDE == localname && XI == prefix) {
@@ -333,48 +353,49 @@
 				writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname);
 			else
 				writer.writeStartElement(localname);
-			
+
 			for (int i = 0; i < parser.getNamespaceCount(); i++) {
 				writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
 			}
-			
+
 			writeAttributes();
 		}
 	}
-	
+
 	protected void writeAttributes() throws XMLStreamException {
 		for (int i = 0; i < parser.getAttributeCount(); i++) {
 			writeAttribute(parser.getAttributePrefix(i), parser.getAttributeLocalName(i), parser.getAttributeValue(i));
 		}
 	}
-	
+
 	protected void writeAttribute(String prefix, String name, String value) throws XMLStreamException {
 		if (prefix != null && prefix.length() > 0)
 			writer.writeAttribute(prefix + ":" + name, value);
 		else
 			writer.writeAttribute(name, value);
 	}
-	
+
 	protected void processCharacters() throws XMLStreamException {
 		writer.writeCharacters(parser.getText());
 	}
-	
+
 	protected void processProcessingInstruction() throws XMLStreamException {
 		writer.writeProcessingInstruction(parser.getPITarget(), parser.getPIData());
+		if (!firstElementStarted) writer.writeCharacters("\n"); // new lines are not reported before the first element is parsed
 	}
-	
+
 	protected void processDTD() throws XMLStreamException {
 		writer.writeDTD(parser.getText());
 	}
-	
+
 	protected void processCDATA() throws XMLStreamException {
 		writer.writeCData(parser.getText());
 	}
-	
+
 	protected void processComment() throws XMLStreamException {
 		writer.writeComment(parser.getText());
 	}
-	
+
 	protected void processEndElement() throws XMLStreamException {
 		if (localname == INCLUDE && parser.getPrefix() == XI) {
 			// nothing !!
@@ -383,15 +404,15 @@
 			writer.writeEndElement();
 		}
 	}
-	
+
 	protected void processEndDocument() throws XMLStreamException {
 		writer.writeEndDocument();
 	}
-	
+
 	protected void processEntityReference() throws XMLStreamException {
 		writer.writeEntityRef(parser.getLocalName());
 	}
-	
+
 	/**
 	 * Process the XInclude elements
 	 * 
@@ -419,7 +440,7 @@
 			System.out.println("Warning referenced file: $ref does not exists");
 		}
 	}
-	
+
 	public static void main(String[] args) {
 		try {
 			File input = new File("/home/mdecorde/xml/identity/test.xml");
@@ -429,9 +450,9 @@
 				return;
 			}
 			StaxIdentityParser builder;
-			
+
 			builder = new StaxIdentityParser(input.toURI().toURL());
-			
+
 			if (builder.process(output)) {
 				System.out.println("success ? " + ValidateXml.test(output));
 			}
