# Buggy code
def transform_reply():
    reply = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="no"/>

    <xsl:template match="/|comment()|processing-instruction()">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    </xsl:stylesheet>
    """
    if sys.version < "3":
        return reply
    else:
        print("utf8")
        return reply.encode("UTF-8")


# Traceback (ansible.module_utils.connection.ConnectionError)
TASK [Enable Netconf service] *************************************************************************************************
task path: /Users/david/code/test/iosxr.yaml:7
<iosxr01> attempting to start connection
<iosxr01> using connection plugin netconf
<iosxr01> found existing local domain socket, using it!
<iosxr01>
<iosxr01> local domain socket path is /Users/david/.ansible/pc/3971a0dcfb
Using module file /Users/david/code/ansible/lib/ansible/modules/network/iosxr/iosxr_netconf.py
<iosxr01> ESTABLISH LOCAL CONNECTION FOR USER: david
<iosxr01> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214 `" &amp;&amp; echo ansible-tmp-1520963067.58-275705164356214="` echo /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214 `" ) &amp;&amp; sleep 0'
<iosxr01> PUT /Users/david/.ansible/tmp/ansible-local-10916PvIHw4/tmpx6yOe7 TO /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/iosxr_netconf.py
<iosxr01> EXEC /bin/sh -c 'chmod u+x /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/ /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/iosxr_netconf.py &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c '/Users/david/code/venv/bin/python /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/iosxr_netconf.py &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c 'rm -f -r /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py", line 203, in <module>
main()
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py", line 188, in main
have = map_config_to_obj(module)
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py", line 130, in map_config_to_obj
netconf_config = get_config(module, config_filter='netconf-yang agent')
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/iosxr/iosxr.py", line 373, in get_config
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py", line 70, in __rpc__
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py", line 102, in parse_rpc_error
ansible.module_utils.connection.ConnectionError: 'NoneType' object has no attribute 'tag'

fatal: [iosxr01]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py\", line 203, in <module>\n    main()\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py\", line 188, in main\n    have = map_config_to_obj(module)\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py\", line 130, in map_config_to_obj\n    netconf_config = get_config(module, config_filter='netconf-yang agent')\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/iosxr/iosxr.py\", line 373, in get_config\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py\", line 70, in __rpc__\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py\", line 102, in parse_rpc_error\nansible.module_utils.connection.ConnectionError: 'NoneType' object has no attribute 'tag'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def transform_reply():
    reply = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="no"/>

    <xsl:template match="/|comment()|processing-instruction()">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    </xsl:stylesheet>
    """
    if sys.version < "3":
        return reply
    else:
        return reply.encode("UTF-8")