docs: public static void writeElement(Element e,
                                      XMLStreamWriter writer,
                                      boolean repairing,
                                      boolean endElement)
          throws XMLStreamException {
          String prefix = e.getPrefix();
          String ns = e.getNamespaceURI();
          String localName = e.getLocalName();
  
          if (prefix == null) {
              prefix = "";
          }
          if (localName == null) {
              localName = e.getNodeName();
  
              if (localName == null) {
                  throw new IllegalStateException("Element's local name cannot be null!");
              }
          }
  
          String decUri = writer.getNamespaceContext().getNamespaceURI(prefix);
          boolean declareNamespace = decUri == null || !decUri.equals(ns);
  
          if (ns == null || ns.length() == 0) {
              writer.writeStartElement(localName);
              if (StringUtils.isEmpty(decUri)) {
                  declareNamespace = false;
              }
          } else {
              writer.writeStartElement(prefix, localName, ns);
          }
  
          for (Node attr : sortElementAttributes(e.getAttributes())) {
  
              String name = attr.getLocalName();
              String attrPrefix = attr.getPrefix();
              if (attrPrefix == null) {
                  attrPrefix = "";
              }
              if (name == null) {
                  name = attr.getNodeName();
              }
  
              if ("xmlns".equals(attrPrefix)) {
                  writer.writeNamespace(name, attr.getNodeValue());
                  writer.setPrefix(name, attr.getNodeValue());
                  if (name.equals(prefix) && attr.getNodeValue().equals(ns)) {
                      declareNamespace = false;
                  }
              } else {
                  if ("xmlns".equals(name) && "".equals(attrPrefix)) {
                      writer.writeDefaultNamespace(attr.getNodeValue());
                      writer.setDefaultNamespace(attr.getNodeValue());
                      if (attr.getNodeValue().equals(ns)) {
                          declareNamespace = false;
                      } else if (StringUtils.isEmpty(attr.getNodeValue())
                                 && StringUtils.isEmpty(ns)) {
                          declareNamespace = false;
                      }
                  } else {
                      String attns = attr.getNamespaceURI();
                      String value = attr.getNodeValue();
                      if (attns == null || attns.length() == 0) {
                          writer.writeAttribute(name, value);
                      } else if (attrPrefix.length() == 0) {
                          writer.writeAttribute(attns, name, value);
                      } else {
                          if (repairing && writer.getNamespaceContext().getNamespaceURI(attrPrefix) == null) {
                              writer.writeNamespace(attrPrefix, attns);
                          }
                          writer.writeAttribute(attrPrefix, attns, name, value);
                      }
                  }
              }
          }
  
          if (declareNamespace && repairing) {
              if (ns == null) {
                  writer.writeNamespace(prefix, "");
                  writer.setPrefix(prefix, "");
              } else {
                  writer.writeNamespace(prefix, ns);
                  writer.setPrefix(prefix, ns);
              }
          }
  
          Node nd = e.getFirstChild();
          while (nd != null) {
              writeNode(nd, writer, repairing);
              nd = nd.getNextSibling();
          }
  
          if (endElement) {
              writer.writeEndElement();
          }
      }