/** * @param el */ public static void pancakeNamespaces(Element el) { Map ns = getParentNamespaces(el); Document d = el.getOwnerDocument(); assert d != null; Iterator it = ns.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); String uri = (String) ns.get(key); Attr a = d.createAttributeNS(NS_URI_XMLNS, (key.length() != 0)?("xmlns:" + key):("xmlns")); a.setValue(uri); el.setAttributeNodeNS(a); } }
public static void copyNSContext(Element source, Element dest) { Map<String, String> sourceNS = getParentNamespaces(source); sourceNS.putAll(getMyNamespaces(source)); Map<String, String> destNS = getParentNamespaces(dest); destNS.putAll(getMyNamespaces(dest)); // (source - dest) to avoid adding twice the same ns on dest for (String pr : destNS.keySet()) sourceNS.remove(pr); for (Map.Entry<String, String> entry : sourceNS.entrySet()) { String prefix = entry.getKey(); String uri = entry.getValue(); if (prefix == null || "".equals(prefix)) dest.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns", uri); else dest.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:"+ prefix, uri); } }
/** * Construct a {@link NSContext} instance for the supplied element. * @param el the <code>Element</code> to gather the namespace context for * @return the <code>NSContext</code> */ public static NSContext getMyNSContext(Element el) { NSContext ns = new NSContext(); ns.register(getParentNamespaces(el)); ns.register(getMyNamespaces(el)); return ns; }