title: How to fix Jackson rendering multiple namespace prefixes for the same namespace
body: <p>I have a pojo which I am serializing using com.fasterxml.jackson.dataformat.xml.XmlMapper.</p>
  
  <p>I have a Java class defined with serveral string values. </p>
  
  <pre><code>@JacksonXmlRootElement(namespace = "http://www.firstnamespace.com", localName = "Student")    
  public class Student {
      @JacksonXmlProperty(namespace = "http://www.firstnamespace.com",localName = "ID")
      public final String id;
      @JacksonXmlProperty(namespace = "http://www.firstnamespace.com",localName = "Name")
      public final String name;
      @JacksonXmlProperty(namespace = "http://www.firstnamespace.com",localName = "Address")
      public Address address;
  }
  </code></pre>
  
  <p>Address is defined as this and the elements of address are in a different namespace</p>
  
  <pre><code>public class Address {
      @JacksonXmlProperty(namespace = "http://www.secondnamespace.com",localName = "StreetNumber")
      public final String streetNumber;
      @JacksonXmlProperty(namespace = "http://www.secondnamespace.com",localName = "StreetName")
      public final String steetName;
  }
  </code></pre>
  
  <p>When I serialize Student with Jackson, I end up with a bunch of repeated namespaces for the address object. </p>
  
  <pre><code>&lt;Student xmlns="http://www.firstnamespace.com"&gt;
      &lt;ID&gt;1234&lt;/ID&gt;
      &lt;Name&gt;Ken&lt;/Name&gt;
      &lt;Address&gt;
          &lt;wstxns1:StreetNumber xmlns:wstnxs1="http://www.secondnamespace.com"&gt;Ken&lt;/wstxns1:StreetNumber&gt;
          &lt;wstxns2:StreetName xmlns:wstnxs2="http://www.secondnamespace.com"&gt;Main Street&lt;/wstxns2:StreetName&gt;
      &lt;/Address&gt;
  </code></pre>
  
  <p>I am serializing the Student class like this:</p>
  
  <pre><code>XmlMapper xmlMapper = new XmlMapper();
  xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
  xmlMapper.writeValue(new File("studentTest.xml", student);
  File file = new File("studentTest.xml");
  </code></pre>
  
  <p>Is there a way to make a single namespace prefix for a given namespace?</p>
  
  <pre><code>&lt;Student xmlns="http://www.firstnamespace.com"&gt;
      &lt;ID&gt;1234&lt;/ID&gt;
      &lt;Name&gt;Ken&lt;/Name&gt;
      &lt;Address&gt;
          &lt;wstxns1:StreetNumber xmlns:wstnxs1="http://www.secondnamespace.com"&gt;Ken&lt;/wstxns1:StreetNumber&gt;
          &lt;wstxns1:StreetName&gt;Main Street&lt;/wstxns1:StreetName&gt;
      &lt;/Address&gt;
  </code></pre>
  
  <p>Or define the namespaces at the top?</p>
  
  <pre><code>&lt;Student xmlns="http://www.firstnamespace.com" xmlns:wstnxs1="http://www.secondnamespace.com"&gt;
      &lt;ID&gt;1234&lt;/ID&gt;
      &lt;Name&gt;Ken&lt;/Name&gt;
      &lt;Address&gt;
          &lt;wstxns1:StreetNumber&gt;Ken&lt;/wstxns1:StreetNumber&gt;
          &lt;wstxns1:StreetName&gt;Main Street&lt;/wstxns1:StreetName&gt;
      &lt;/Address&gt;
  </code></pre>
  
  <p>I have read that I need to use Woodstox to help with this problem and I have tried the following but I don't see how Woodstox configuration helps fix this. I was able to change the prefix however:</p>
  
  <pre><code>XMLInputFactory2  inputFactory2 =  new WstxInputFactory();
  XMLOutputFactory2 outputFactory2 = new WstxOutputFactory();
  
  outputFactory2.setProperty(XMLOutputFactory2.P_AUTOMATIC_NS_PREFIX,"foo");
  outputFactory2.configureForRobustness();
  XmlFactory xmlFactory = new XmlFactory(inputFactory2,outputFactory2);
  XmlMapper xmlMapper = new XmlMapper(xmlFactory);
  </code></pre>