Search code examples
pythonxmlxsltxpathnonetype

TypeError: 'NoneType' does not support the buffer interface: when Transforming XML using XSLT


Related to earlier question, I want to thank @Parfait for helping me develop an XSLT foundation for my project with the current XSLT code being used. However I am getting an error when I run the python Module it says.

File "nsnindex.py", line 17, in <module>
  xmlfile.write(tree_out)
TypeError: 'NoneType' does not support the buffer interface.

The overall plan is for XPATH to go straight to the third child node

<national_stock_number_cross_reference_index>

and use the data in that section to output to a new file. At the moment, python spits out all of the data in one blob, without tags, and gives me the error above.

XML Input

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<lsar030 xsi:schemaLocation="places" xmlns="place2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <head>
        <requester>Guy</requester>
        <time>12:23:44 PM</time>
        <date>Aug 2, 2016</date>
        <eiac>ABC</eiac>
        <item_name>Thing</item_name>
        <start_lcn>start34</start_lcn>
        <alc>00</alc>
        <lcn_type>P</lcn_type>
        <stop_lcn>AA01AA13AB</stop_lcn>
        <useable_on_code>ABC</useable_on_code>
        <technical_manual_code>000</technical_manual_code>
        <technical_manual_number>15241</technical_manual_number>
        <report_format>STUFF</report_format>
        <kit_option>45</kit_option>
        <part_number_cross_reference_index>true</part_number_cross_reference_index>
        <national_stock_number_cross_reference_index>true</national_stock_number_cross_reference_index>
        <reference_designation_cross_reference_index>false</reference_designation_cross_reference_index>
        <figure_item_number_cross_reference_index>false</figure_item_number_cross_reference_index>
        <include_drawings_in_output>No</include_drawings_in_output>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 204</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 205</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 208</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 207</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>11</technical_manual_functional_group_code>
            <header> FIGURE 501 </header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>12</technical_manual_functional_group_code>
            <header>FIGURE 505</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 201</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 202</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 203/header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 200</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>865/technical_manual_functional_group_code>
            <header>FIGURE 503</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>865</technical_manual_functional_group_code>
            <header>FIGURE 502</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>865</technical_manual_functional_group_code>
            <header>FIGURE 504</header>
        </technical_manual_functional_group_code_header>
        <technical_manual_functional_group_code_header>
            <section>2</section>
            <technical_manual_functional_group_code>132</technical_manual_functional_group_code>
            <header>FIGURE 206/header>
        </technical_manual_functional_group_code_header>
        <baseline_comparison>false</baseline_comparison>
        <usmc_option>No</usmc_option>
    </head>
    <body>
        <repair_parts_list>
            <repair_parts_fgc_group>             
                <repair_part>
                    <item_number>1</item_number>
                    <smr>PAFDD</smr>
                    <cage_code>53711</cage_code>
                    <reference_number>7102539</reference_number>
                    <technical_manual_indenture_code>1</technical_manual_indenture_code>
                    <item_name>DINGLE</item_name>
                    <useable_on_code>EAU</useable_on_code>
                    <fully_effective>true</fully_effective>
                    <quantity>1</quantity>
                    <qpei>1</qpei>
                    <figure_number>101</figure_number>
                    <technical_manual_functional_group_code>0000000000A</technical_manual_functional_group_code>
                    <federal_supply_classification>1234</federal_supply_classification>
                    <national_item_identification_number>000000000</national_item_identification_number>
                    <system_or_end_item_provisioning_contract_control_number>A1ABC</system_or_end_item_provisioning_contract_control_number>
                    <plisn>ABCD</plisn>
                    <effectivity_flag>false</effectivity_flag>
                    <effectivity_label>BERRY</effectivity_label>
                </repair_part>
            </repair_parts_fgc_group>
        </repair_parts_list>
        <part_number_cross_reference_index>
            <work_package_sequence_number>0005</work_package_sequence_number>
            <part_number_cross_reference>
                <reference_number>XXXXXX</reference_number>
                <figure_number>1</figure_number>
                <item_number>10</item_number>
            </part_number_cross_reference>
        </part_number_cross_reference_index>
        <national_stock_number_cross_reference_index>
            <work_package_sequence_number>0001</work_package_sequence_number>
            <national_stock_number_cross_reference>
                <federal_supply_classification>1234</federal_supply_classification>
                <national_item_identification_number>000000000</national_item_identification_number>
                <figure_number>1</figure_number>
                <item_number>1</item_number>
            </national_stock_number_cross_reference><national_stock_number_cross_reference>
                <federal_supply_classification>1234</federal_supply_classification>
                <national_item_identification_number>000000000</national_item_identification_number>
                <figure_number>2</figure_number>
                <item_number>4</item_number>
            </national_stock_number_cross_reference><national_stock_number_cross_reference>
                <federal_supply_classification>1234</federal_supply_classification>
                <national_item_identification_number>000000000</national_item_identification_number>
                <figure_number>3</figure_number>
                <item_number>2</item_number>
            </national_stock_number_cross_reference><national_stock_number_cross_reference>
                <federal_supply_classification>1235</federal_supply_classification>
                <national_item_identification_number>111111111</national_item_identification_number>
                <figure_number>1</figure_number>
                <item_number>1</item_number>
            </national_stock_number_cross_reference><national_stock_number_cross_reference>
                <federal_supply_classification>1235</federal_supply_classification>
                <national_item_identification_number>111111111</national_item_identification_number>
                <figure_number>2</figure_number>
                <item_number>3</item_number>
            </national_stock_number_cross_reference>
        </national_stock_number_cross_reference_index><national_stock_number_cross_reference>
                <federal_supply_classification>1236</federal_supply_classification>
                <national_item_identification_number>212121212</national_item_identification_number>
                <figure_number>2</figure_number>
                <item_number>3</item_number>
            </national_stock_number_cross_reference>
        </national_stock_number_cross_reference_index>
    </body>
</lsar030>

XSLT Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

 <xsl:key name="numkey" match="lsar030/body/national_stock_number_cross_reference_index/national_stock_number_cross_reference"
          use="concat(federal_supply_classification, national_item_identification_number)"/>

  <xsl:template match="lsar030/body/national_stock_number_cross_reference_index">
      <xsl:apply-templates select="national_stock_number_cross_reference[generate-id() =
           generate-id(key('numkey', concat(federal_supply_classification, national_item_identification_number))[1])]"/>    
  </xsl:template>

 <xsl:template match="lsar030/body/national_stock_number_cross_reference_index/national_stock_number_cross_reference">
  <nsnindxrow>
    <nsn> 
      <fsc><xsl:value-of select="federal_supply_classification"/></fsc>
      <niin><xsl:value-of select="national_item_identification_number"/></niin>
    </nsn>
    <xsl:for-each select="key('numkey', concat(federal_supply_classification, national_item_identification_number))">
      <callout>
        <xsl:attribute name="assocfig"><xsl:value-of select="concat('fig', figure_number)"/></xsl:attribute>
        <xsl:attribute name="label"><xsl:value-of select="item_number"/></xsl:attribute>
      </callout>
    </xsl:for-each>
  </nsnindxrow>
 </xsl:template>

</xsl:stylesheet>

Python Code:

import lxml.etree as ET

# LOAD XML AND XSL FILES
dom = ET.parse('lsar030.xml')
xslt = ET.parse('XSLTScript.xsl')

# TRANSFORM SOURCE
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(newdom)

# OUTPUT TRANSFORMED TREE TO STRING
tree_out = ET.tostring(newdom, encoding='UTF-8', pretty_print=True, xml_declaration=True)

# OUTPUT STRING TO FILE
xmlfile = open('Output.xml', 'wb')
xmlfile.write(tree_out)
xmlfile.close()

OUTPUT

<nsnindxrow>
<nsn>
<fsc>5310</fsc>
<niin>00-880-5978</niin>
</nsn>
<callout assocfig="fig700" label="34">
<callout assocfig="fig701" label="10">
<callout assocfig="fig703" label="9">
</nsnindxrow>

Anyone know what I am doing wrong? is it my XPATH? @Parfait 's program worked when the root tag was just <national_stock_number_cross_reference_index>, but it stopped working properly once I changed the path to match the more complex file.


Solution

  • Two issues in XML emerge affecting the XSLT transformation:

    1. An undeclared namespace prefix (xmlns="place2") where the xmlns special attribute in header does not have a colon separated identifier. Therefore, XSLT must declare a prefix.
    2. Additional element levels where parent nodes and their siblings above national_stock_number_cross_reference_index element must be parsed out. Therefore, XSLT must use additional templates.

    Consider following adjusted XSLT, declaring a doc prefix which is prefixed in all other XPath expressions. Use same Python code to process.

    XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                  xmlns:doc="place2" exclude-result-prefixes="doc">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
     <xsl:key name="numkey" match="doc:national_stock_number_cross_reference"
              use="concat(doc:federal_supply_classification, doc:national_item_identification_number)"/>
    
      <xsl:template match="/doc:lsar030">
        <xsl:apply-templates select="doc:head"/>
        <xsl:apply-templates seect="doc:body"/>
      </xsl:template>
    
      <xsl:template match="doc:head"/>
    
      <xsl:template match="doc:body">
        <xsl:apply-templates select="doc:national_stock_number_cross_reference_index"/>
      </xsl:template>
    
      <xsl:template match="doc:national_stock_number_cross_reference_index">  
        <nsnindxrows>  
          <xsl:apply-templates select="doc:national_stock_number_cross_reference[generate-id() =
               generate-id(key('numkey', concat(doc:federal_supply_classification, doc:national_item_identification_number))[1])]"/>    
        <nsnindxrows>
      </xsl:template>
    
     <xsl:template match="doc:national_stock_number_cross_reference">
      <nsnindxrow>
        <nsn> 
          <fsc><xsl:value-of select="doc:federal_supply_classification"/></fsc>
          <niin><xsl:value-of select="doc:national_item_identification_number"/></niin>
        </nsn>
        <xsl:for-each select="key('numkey', concat(doc:federal_supply_classification, doc:national_item_identification_number))">
          <callout>
            <xsl:attribute name="assocfig"><xsl:value-of select="concat('fig', doc:figure_number)"/></xsl:attribute>
            <xsl:attribute name="label"><xsl:value-of select="doc:item_number"/></xsl:attribute>
          </callout>
        </xsl:for-each>
      </nsnindxrow>  
     </xsl:template>
    
    </xsl:stylesheet>
    

    Output

    (unlike earlier question's XML, only one callout renders due to this XML's one pairing of <federal_supply_classification> and <national_item_identification_number>).

    <?xml version="1.0"?>
    <nsnindxrows>
      <nsnindxrow>
        <nsn>
          <fsc>1234</fsc>
          <niin>000000000</niin>
        </nsn>
        <callout assocfig="fig1" label="1"/>
        <callout assocfig="fig2" label="4"/>
        <callout assocfig="fig3" label="2"/>
      </nsnindxrow>
      <nsnindxrow>
        <nsn>
          <fsc>1235</fsc>
          <niin>111111111</niin>
        </nsn>
        <callout assocfig="fig1" label="1"/>
        <callout assocfig="fig2" label="3"/>
      </nsnindxrow>
      <nsnindxrow>
        <nsn>
          <fsc>1236</fsc>
          <niin>212121212</niin>
        </nsn>
        <callout assocfig="fig2" label="3"/>
      </nsnindxrow>
    </nsnindxrows>