/**
   * It is expected that a Docbook 5 book or article will define the namespaces at the root element.
   * But for validation against a single topic, we need to add these namespaces in in order for the
   * dtd to validate.
   *
   * @param xml The source xml
   * @return the xml with the docbook 5 namespaces added
   */
  public static String addDocBook50Namespaces(@NotNull final String xml) {
    final String rootEleName = XMLUtilities.getRootElementName(xml);
    final String fixedRootEleName = rootEleName == null ? "section" : rootEleName;
    final MatchResult result =
        RegExp.compile("^([\\s\\S]*?)<\\s*" + fixedRootEleName + "(\\s*.*?)>").exec(xml);
    if (result != null) {
      final StringBuilder retValue = new StringBuilder(result.getGroup(1));
      retValue.append("<" + fixedRootEleName);

      // Clean out the normal attributes
      String fixedAttributes = result.getGroup(2);
      // Remove any current namespace declaration
      fixedAttributes = fixedAttributes.replaceFirst(" xmlns\\s*=\\s*('|\").*?('|\")", "");
      // Remove any current version declaration
      fixedAttributes = fixedAttributes.replaceFirst(" version\\s*=\\s*('|\").*?('|\")", "");
      // Remove any current xlink namespace declaration
      fixedAttributes = fixedAttributes.replaceFirst(" xmlns:xlink\\s*=\\s*('|\").*?('|\")", "");

      // Add the generic attributes
      retValue.append(" xmlns=\"http://docbook.org/ns/docbook\"");
      retValue.append(" version=\"5.0\"");
      if (!rootEleName.equalsIgnoreCase("info")) {
        // Info is not allowed to have xlink declared for the DocBook 5.0 DTD
        retValue.append(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"");
      }

      retValue.append(fixedAttributes + ">");
      return xml.replace(result.getGroup(0), retValue.toString());
    }

    return xml;
  }