def serialize_gen(
    obj_pyxb, encoding='utf-8', pretty=False, strip_prolog=False, xslt_url=None
):
    """Serialize PyXB object to XML.

    Args:
      obj_pyxb: PyXB object
        PyXB object to serialize.

      encoding: str
        Encoding to use for XML doc bytes

      pretty: bool
        True: Use pretty print formatting for human readability.

      strip_prolog:
        True: remove any XML prolog (e.g., ``<?xml version="1.0" encoding="utf-8"?>``),
        from the resulting XML doc.

      xslt_url: str
        If specified, add a processing instruction to the XML doc that specifies the
        download location for an XSLT stylesheet.

    Returns:
      XML document

    """
    assert d1_common.type_conversions.is_pyxb(obj_pyxb)
    assert encoding in (None, 'utf-8', 'UTF-8')
    try:
        obj_dom = obj_pyxb.toDOM()
    except pyxb.ValidationError as e:
        raise ValueError(
            'Unable to serialize PyXB to XML. error="{}"'.format(e.details())
        )
    except pyxb.PyXBException as e:
        raise ValueError('Unable to serialize PyXB to XML. error="{}"'.format(str(e)))

    if xslt_url:
        xslt_processing_instruction = obj_dom.createProcessingInstruction(
            'xml-stylesheet', 'type="text/xsl" href="{}"'.format(xslt_url)
        )
        root = obj_dom.firstChild
        obj_dom.insertBefore(xslt_processing_instruction, root)

    if pretty:
        xml_str = obj_dom.toprettyxml(indent='  ', encoding=encoding)
        # Remove empty lines in the result caused by a bug in toprettyxml()
        if encoding is None:
            xml_str = re.sub(r'^\s*$\n', r'', xml_str, flags=re.MULTILINE)
        else:
            xml_str = re.sub(b'^\s*$\n', b'', xml_str, flags=re.MULTILINE)
    else:
        xml_str = obj_dom.toxml(encoding)
    if strip_prolog:
        if encoding is None:
            xml_str = re.sub(r'^<\?(.*)\?>', r'', xml_str)
        else:
            xml_str = re.sub(b'^<\?(.*)\?>', b'', xml_str)

    return xml_str.strip()
Serialize PyXB object to XML.

    Args:
      obj_pyxb: PyXB object
        PyXB object to serialize.

      encoding: str
        Encoding to use for XML doc bytes

      pretty: bool
        True: Use pretty print formatting for human readability.

      strip_prolog:
        True: remove any XML prolog (e.g., ``<?xml version="1.0" encoding="utf-8"?>``),
        from the resulting XML doc.

      xslt_url: str
        If specified, add a processing instruction to the XML doc that specifies the
        download location for an XSLT stylesheet.

    Returns:
      XML document