Steven Steven - 3 months ago 10
Python Question

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>
</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.

Answer

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 seect="doc:body"/>
  </xsl:template>

  <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"?>
<nsnindxrow>
  <nsn>
    <fsc>1234</fsc>
    <niin>000000000</niin>
  </nsn>
  <callout assocfig="fig1" label="1"/>
</nsnindxrow>
Comments