Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JAXB always adds all namespaces that are known by the JAXBContext to the root element of the XML document for performance reasons. See this comment by Kohsuke on <a href="https://github.com/javaee/jaxb-v2/issues/103#issuecomment-296633602" rel="nofollow noreferrer">JAXB-103</a> for more information.</p> <p>The only way I found to deal with this, is to traverse the document myself after it has been created with JAXB and remove all unused namespaces using the following helper class:</p> <pre><code>public class RemoveUnusedNamespaces { private static final String XML_NAMESPACE_SCHEMA_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance"; private static final String XML_NAMESPACE_NAMESPACE = "http://www.w3.org/2000/xmlns/"; private interface ElementVisitor { void visit(Element element); } public void process(Document document) { final Set&lt;String&gt; namespaces = new HashSet&lt;String&gt;(); Element element = document.getDocumentElement(); traverse(element, new ElementVisitor() { public void visit(Element element) { String namespace = element.getNamespaceURI(); if (namespace == null) namespace = ""; namespaces.add(namespace); NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i &lt; attributes.getLength(); i++) { Node node = attributes.item(i); if (XML_NAMESPACE_NAMESPACE.equals(node.getNamespaceURI())) continue; String prefix; if (XML_NAMESPACE_SCHEMA_INSTANCE.equals(node.getNamespaceURI())) { if ("type".equals(node.getLocalName())) { String value = node.getNodeValue(); if (value.contains(":")) prefix = value.substring(0, value.indexOf(":")); else prefix = null; } else { continue; } } else { prefix = node.getPrefix(); } namespace = element.lookupNamespaceURI(prefix); if (namespace == null) namespace = ""; namespaces.add(namespace); } } }); traverse(element, new ElementVisitor() { public void visit(Element element) { Set&lt;String&gt; removeLocalNames = new HashSet&lt;String&gt;(); NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i &lt; attributes.getLength(); i++) { Node node = attributes.item(i); if (!XML_NAMESPACE_NAMESPACE.equals(node.getNamespaceURI())) continue; if (namespaces.contains(node.getNodeValue())) continue; removeLocalNames.add(node.getLocalName()); } for (String localName : removeLocalNames) element.removeAttributeNS(XML_NAMESPACE_NAMESPACE, localName); } }); } private final void traverse(Element element, ElementVisitor visitor) { visitor.visit(element); NodeList children = element.getChildNodes(); for (int i = 0; i &lt; children.getLength(); i++) { Node node = children.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; traverse((Element) node, visitor); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload