protected static org.w3c.dom.Attr getNamespaceAttrFrom(Element element, String prefix) { NamespaceContextIterator eachNamespace = new NamespaceContextIterator(element); while (eachNamespace.hasNext()) { org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr(); String declaredPrefix = NameImpl.getLocalNameFromTagName(namespaceDecl.getNodeName()); if (declaredPrefix.equals(prefix)) { return namespaceDecl; } } return null; }
protected org.w3c.dom.Attr getNamespaceAttr(String prefix) { NamespaceContextIterator eachNamespace = getNamespaceContextNodes(); if (!"".equals(prefix)) prefix = ":" + prefix; while (eachNamespace.hasNext()) { org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr(); if (!"".equals(prefix)) { if (namespaceDecl.getNodeName().endsWith(prefix)) return namespaceDecl; } else { if (namespaceDecl.getNodeName().equals("xmlns")) return namespaceDecl; } } return null; }
public String getNamespacePrefix(String uri) { NamespaceContextIterator eachNamespace = getNamespaceContextNodes(); while (eachNamespace.hasNext()) { org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr(); if (namespaceDecl.getNodeValue().equals(uri)) { String candidatePrefix = namespaceDecl.getLocalName(); if ("xmlns".equals(candidatePrefix)) return ""; else return candidatePrefix; } } // Find if any of the ancestors' name has this uri org.w3c.dom.Node currentAncestor = this; while (currentAncestor != null && !(currentAncestor instanceof Document)) { if (uri.equals(currentAncestor.getNamespaceURI())) return currentAncestor.getPrefix(); currentAncestor = currentAncestor.getParentNode(); } return null; }