title: Validation fails when writing XML with Woodstox / Stax2
body: <p>I'm having an issue with XML validation using an XSD schema with Woodstox and Stax2. Validation fails even though the XML data conforms to the schema.</p>
  
  <p>Surprisingly, the validation issue only occurs when <em>writing</em> XML (using XMLStreamWriter2), not when <em>reading</em> XML (using XMLStreamReader2).</p>
  
  <p>I've built a small example to reproduce and isolate the error.
  Basically, it just reads XML from a file into an XMLStreamReader2 (validating with an XSD schema), then copy it to an XMLStreamWriter2 (also validating with the same XSD).</p>
  
  <p>Now, this fails with a validation error from the <em>writer</em>. If I deactivate validation on the writer, everything goes smoothly and the writer delivers perfectly conform XML.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import com.ctc.wstx.stax.WstxInputFactory;
  import com.ctc.wstx.stax.WstxOutputFactory;
  import org.codehaus.stax2.XMLStreamReader2;
  import org.codehaus.stax2.XMLStreamWriter2;
  import org.codehaus.stax2.validation.XMLValidationSchema;
  import org.codehaus.stax2.validation.XMLValidationSchemaFactory;
  
  import javax.xml.stream.XMLStreamException;
  import java.io.InputStream;
  import java.io.StringWriter;
  
  public class Converter {
  
      public static void main(String... args) throws XMLStreamException {
  
          InputStream reader = Converter.class.getClassLoader().getResourceAsStream("test.xml");
          StringWriter writer = new StringWriter();
  
          XMLValidationSchema schema = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA)
                  .createSchema(Converter.class.getClassLoader().getResourceAsStream("schema.xsd"));
  
  
          XMLStreamReader2 xmlReader = (XMLStreamReader2) new WstxInputFactory().createXMLStreamReader(reader);
          xmlReader.validateAgainst(schema);
  
          XMLStreamWriter2 xmlWriter = (XMLStreamWriter2) new WstxOutputFactory().createXMLStreamWriter(writer);
          xmlWriter.validateAgainst(schema);
  
          xmlWriter.copyEventFromReader(xmlReader, false);
  
          while (xmlReader.hasNext()) {
              xmlReader.next();
  
              xmlWriter.copyEventFromReader(xmlReader, false);
          }
  
          System.out.println(writer.toString());
      }
  }
  </code></pre>
  
  <p>Here is the XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;JobStatus xsdVersion="NA"&gt;
      &lt;Document&gt;
          &lt;DocumentId&gt;1234567890&lt;/DocumentId&gt;
      &lt;/Document&gt;
      &lt;Document&gt;
          &lt;DocumentId&gt;1234567891&lt;/DocumentId&gt;
      &lt;/Document&gt;
  &lt;/JobStatus&gt;
  </code></pre>
  
  <p>And here is the schema:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema elementFormDefault="qualified"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  
      &lt;xs:element name="JobStatus"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="Document" maxOccurs="unbounded"&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element name="DocumentId" type="xs:string"/&gt;
                          &lt;/xs:sequence&gt;
                      &lt;/xs:complexType&gt;
                  &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
              &lt;xs:attribute name="xsdVersion" type="xs:string" use="required"/&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>And this all result in (with validation on the writer enabled):</p>
  
  <pre><code>Exception in thread "main" com.ctc.wstx.exc.WstxValidationException: element "JobStatus" is missing "xsdVersion" attribute
          at [row,col {unknown-source}]: [1,66]
          at com.ctc.wstx.exc.WstxValidationException.create(WstxValidationException.java:50)
          at com.ctc.wstx.sw.BaseStreamWriter.reportProblem(BaseStreamWriter.java:1223)
          at com.ctc.wstx.msv.GenericMsvValidator.reportError(GenericMsvValidator.java:549)
          at com.ctc.wstx.msv.GenericMsvValidator.reportError(GenericMsvValidator.java:541)
          at com.ctc.wstx.msv.GenericMsvValidator.reportError(GenericMsvValidator.java:535)
          at com.ctc.wstx.msv.GenericMsvValidator.validateElementAndAttributes(GenericMsvValidator.java:343)
          at com.ctc.wstx.sw.BaseNsStreamWriter.closeStartElement(BaseNsStreamWriter.java:420)
          at com.ctc.wstx.sw.BaseStreamWriter.copyEventFromReader(BaseStreamWriter.java:807)
          at Converter.main(Converter.java:34)
  </code></pre>
  
  <p>Without validation on the writer, the program runs fine and returns the same XML provided as input (modulo some indentation and line break differences)</p>
  
  <p>So my question is: am I doing something wrong with Woodstox here? Why does validation fail only on the writer ?</p>
  
  <p>I can reproduce this issue with other pairs of XSD and XML, in which case you can get different kind of errors, but always on writer side. Validation on the reader side always work (as long as the XML conforms to the XSD obviously).</p>
  
  <p>Any insights would be greatly appreciated !</p>
  
  <p>PS: for reference, here are the dependencies and version the example uses</p>
  
  <ul>
  <li>org.codehaus.woodstox stax2-api 4.0.0</li>
  <li>com.fasterxml.woodstox woodstox-core 5.0.2</li>
  <li>net.java.dev.msv msv-core 2013.6.1</li>
  <li>net.java.dev.msv xsdlib 2013.6.1&lt;</li>
  </ul>