MOBI_RET mobi_xml_write_reference(xmlTextWriterPtr writer, const OPFreference **reference) {
    if (reference) {
        size_t i = 0;
        while (i < OPF_META_MAX_TAGS) {
            if (reference[i] == NULL) {
                break;
            }
            int xml_ret;
            xml_ret = xmlTextWriterStartElement(writer, BAD_CAST "reference");
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST reference[i]->type);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "title", BAD_CAST reference[i]->title);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "href", BAD_CAST reference[i]->href);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterEndElement(writer);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            i++;
        }
    }
    return MOBI_SUCCESS;
}
MOBI_RET mobi_xml_write_reference(xmlTextWriterPtr writer, const OPFreference **reference) {
    if (reference) {
        size_t i = 0;
        while (i < OPF_META_MAX_TAGS) {
            if (reference[i] == NULL) {
                break;
            }
            int xml_ret;
            xml_ret = xmlTextWriterStartElement(writer, BAD_CAST "reference");
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "type", BAD_CAST reference[i]->type);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "title", BAD_CAST reference[i]->title);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterWriteAttribute(writer, BAD_CAST "href", BAD_CAST reference[i]->href);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            xml_ret = xmlTextWriterEndElement(writer);
            if (xml_ret < 0) {
                debug_print("XML error: %i (reference type: %s)\n", xml_ret, reference[i]->type);
                return MOBI_XML_ERR;
            }
            i++;
        }
    }
    return MOBI_SUCCESS;
}