0

I can easily add attribute namespace for xmlns:lc But how to set attribute namespace for xml:lang This is how i set xmlns:lc:

Document doc = createEmptyDocument();
doc.normalizeDocument();
Element pubElement = doc.createElement("publication");
pubElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:lc", "http://.../lc");
pubElement.setAttributeNS(XMLConstants.XML_NS_URI, "xml:lang", "http://www.w3.org/XML/2000/10/xml.xsd");

I've search in stackoverflow about this question, but none of the answers helped

slavov
  • 630
  • 7
  • 22
  • Have you tried just [setting an attribute](https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Element.html#setAttribute(java.lang.String,%20java.lang.String)), `pubElement.setAttribute("xml:lang", "yourLang");`? – António Ribeiro Feb 22 '16 at 11:55
  • yes, i've tryed. But did not work for me. What i've also tried is `doc.createElementNS(XMLConstants.XML_NS_URI, "xml:lang"); pubElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xml",XMLConstants.XML_NS_URI); pubElement.setAttributeNS(XMLConstants.XML_NS_URI, "xml:lang", lang); pubElement.setAttribute("xml:lang", "http://www.w3.org/XML/1998/namespace");` – slavov Feb 22 '16 at 12:00
  • I think that you are trying to create attribute with name `"xmlns:lc"` in namespace `"http://www.w3.org/2000/xmlns/"`; that's not a valid attribute name. You should just use the attribute name without namespace prefix, i.e. `pubElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "lc", "http://.../lc");` and `pubElement.setAttributeNS(XMLConstants.XML_NS_URI, "lang", "http://www.w3.org/XML/2000/10/xml.xsd");`. (The two prefixes are already associated with the namespace name by default.) – Mike Rosoft Jan 08 '21 at 13:32

0 Answers0