title: Jaxb2Marshaller schema validation doesn't seem to be working
body: <p>I experienced that Jaxb2Marshaller failed to validate my invalid XML against the XSD while doing the unmarshalling. 
  I'm using Spring 4.0.0.RELEASE and Java 7.</p>
  
  <p>Here is an example:</p>
  
  <p>The XSD file: <strong>fruit.xsd</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified"&gt;
  
      &lt;xs:attribute name="quantity" type="xs:string" /&gt;
  
      &lt;xs:element name="fruit"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:choice&gt;
                      &lt;xs:element ref="banana" /&gt;
                      &lt;xs:element ref="apple" /&gt;
                  &lt;/xs:choice&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="banana"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:attribute ref="quantity" use="required" /&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:element name="apple"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:attribute ref="quantity" use="required" /&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>I generated the JAXB POJOs from this XSD with Spring Tool Suite to <code>com.testraptor.xml.jaxb</code> package.</p>
  
  <p>My invalid XML file: <strong>invalid.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;fruit&gt;
      &lt;banana quantity="5" /&gt;
      &lt;apple quantity="3" /&gt;
  &lt;/fruit&gt;
  </code></pre>
  
  <p>As you can see I broke the schema because there is a choice in the fruit tag and I used banana and apple at the same time.</p>
  
  <p>My Spring configuration file: <strong>app-config.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:oxm="http://www.springframework.org/schema/oxm"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/oxm 
      http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd"&gt;
  
      &lt;oxm:jaxb2-marshaller id="marshaller" context-path="com.fruit.xml.jaxb" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>The main class to test: <strong>Main.java</strong></p>
  
  <pre><code>package com.fruit.test;
  
  import java.io.IOException;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Unmarshaller;
  import javax.xml.transform.stream.StreamSource;
  import javax.xml.validation.Schema;
  import javax.xml.validation.SchemaFactory;
  
  import org.springframework.oxm.jaxb.Jaxb2Marshaller;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import org.springframework.core.io.Resource;
  import org.xml.sax.SAXException;
  
  import com.fruit.xml.jaxb.Fruit;
  
  public class Main {
  
      public static void main(String[] args) {
          ApplicationContext appContext = new ClassPathXmlApplicationContext(
                  "app-context.xml");
  
          //--------------------------------SCHEMA VALIDATION IS OMITTED
  
          Jaxb2Marshaller jaxb2Unmarshaller = (Jaxb2Marshaller) appContext
                  .getBean("marshaller");
          Resource schemaResource = appContext
                  .getResource("classpath:fruit.xsd");
          jaxb2Unmarshaller.setSchema(schemaResource);
  
          Resource xml = appContext.getResource("classpath:invalid.xml");
  
          try {
              Fruit fruit = (Fruit) jaxb2Unmarshaller.unmarshal(new StreamSource(xml.getInputStream()));
              System.out.println("SCHEMA VALIDATION IS OMITTED:");
              System.out.println("Apple quantity is " + fruit.getApple().getQuantity());
              System.out.println("Banana quantity is " + fruit.getBanana().getQuantity());
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          //--------------------------------SCHEMA VALIDATION IS PASSED
          SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
          Schema schema;
          try {
              schema = sf.newSchema(schemaResource.getURL());
              JAXBContext context = JAXBContext.newInstance(Fruit.class);
              Unmarshaller unmarshaller = (Unmarshaller) context.createUnmarshaller(); 
              unmarshaller.setSchema(schema); 
              Fruit fruit2 = (Fruit) unmarshaller.unmarshal(xml.getInputStream());
              System.out.println("SCHEMA VALIDATION IS PASSED:");
              System.out.println("Apple quantity is " + fruit2.getApple().getQuantity());
              System.out.println("Banana quantity is " + fruit2.getBanana().getQuantity());
          } catch (SAXException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (JAXBException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } 
      }
  
  }
  </code></pre>
  
  <p>When I run the code I get the following result:</p>
  
  <pre><code>INFO: Loading XML bean definitions from class path resource [app-context.xml]
  dec. 26, 2013 9:33:22 DE org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromContextPath
  INFO: Creating JAXBContext with context path [com.fruit.xml.jaxb]
  SCHEMA VALIDATION IS OMITTED:
  Apple quantity is 3
  Banana quantity is 5
  javax.xml.bind.UnmarshalException
   - with linked exception:
  [org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 24; cvc-complex-type.2.4.d: Invalid content was found starting with element 'apple'. No child element is expected at this point.]
      at ...
  </code></pre>
  
  <p>As you can see the Jaxb2Marshaller forgot to validate my invalid XML file while the Unmarshaller could validate it.</p>
  
  <p>Can anyone give me a clue what could be wrong with my code?</p>