Generate CSV report with xsltproc

KiCad still have xsltproc which can be used to process netlist and generate various outputs. You only need to find or write stylesheet.
http://docs.kicad.org/stable/en/eeschema.html#xslt-approach

1 Like

okay…thank you…

Here is example script:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE xsl:stylesheet [
  <!ENTITY nl  "&#xd;&#xa;"> <!--new line CR, LF -->
]>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

<xsl:template match="/export">
    <xsl:apply-templates select="nets/net"/>
</xsl:template>

<!-- for each net -->
<xsl:template match="net">
    <xsl:choose>
        <xsl:when test = "@name != '' ">
            <xsl:value-of select="@name"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>N-</xsl:text>
            <xsl:value-of select="@code"/>
        </xsl:otherwise>
    </xsl:choose>
    <xsl:text>;</xsl:text>
    <xsl:apply-templates select="node"/>
	<xsl:text>;&nl;</xsl:text>
</xsl:template>

<!-- for each node -->
<xsl:template match="node">
    <xsl:text> </xsl:text>
    <xsl:value-of select="@ref"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="@pin"/>
</xsl:template>

</xsl:stylesheet>

which can be used to create CSV file with all nets and nodes. Example output:

Net-(Q10-Pad4); R29.2 Q10.4 Q11.3;
Net-(Q11-Pad1); R32.1 Q11.1 R31.1;
Net-(Q12-Pad1); Q12.1 R33.1 R34.1;
Net-(Q12-Pad3); Q12.3 Q13.2 R35.1;
Net-(P13-Pad2); Q13.8 P13.2 Q13.7;
Net-(P13-Pad3); Q13.5 Q13.6 P13.3;
Net-(P11-Pad3); P11.3 Q10.6 Q10.5;
Net-(Q10-Pad2); R27.1 Q8.3 Q10.2 Q9.3;
Net-(P11-Pad2); P11.2 Q10.7 Q10.8;
1 Like

This script above looks very useful. But i have no idea how to run it. Can anyone help me with it ?

Generally it’s not a good idea to revive an old topic. However the technique is still relevant so I’ve moved further posts here. The answer is in the first post of this thread; you need the xsltproc program. You arrange to feed the XML netlist from the Schematic Editor or the PCB Editor to xsltproc, supplying the above XSLT stylesheet as the first argument. Look at the documentation for xsltproc for further instructions.

XSLT isn’t the only way to do it. These days many people use Python scripts. In fact any programming language could be used, you read in XML and output it the way you want. There may already be scripts out there that do what you want.

Actually the link in the first post gives a 404, not sure if it the docs got reorganised or XSLT is passé.

Removing XSLT support has been at least discussed IIRC, although it was about BOM and I’m not sure if it affects netlists directly. They share some features. I think the goal is to have a better dialog for creating BOM, and the rest can be done with python.

As for the question: you have to add a “Generator” in the Export Netlist dialog. Browse generators and choose the xml file. For me it creates the command line automatically and then it’s added as a tab to the dialog.

There is no devpower spent in providing XSLT since it’s done by an external program which is maintained by another project. On Linux it’s provided by a separate package. Only issue is whether to make it a dependency package. Dunno about the other 2 platforms.

You don’t have to use a generator, it’s possible to export the XML then process it. Now with kicad-cli it might even be scripted. (Unverified.)

You need to make it clear to people first of all) So that when you click the specification there is a ready-made table and not a test file that still needs to be transformed

Hi. The xsltproc appears to have been removed from KiCAD 7. Here is a solution:

Keep the instructions currently, but one slight change. Create a new .py file with the same name and put it in the same folder as bom2grouped_csv_jlcpcb.xsl and create the file with:

import lxml.etree as ET
import csv
import sys

# Get the command line arguments
xml_path = sys.argv[1]
csv_path = sys.argv[2]

def generate_bom(xml_path, csv_path):
# Load the EESCHEMA XML Partlist Format file
tree = ET.parse(xml_path)
root = tree.getroot()

# Define the namespace for the XML file
ns = {"xsl": "http://www.w3.org/1999/XSL/Transform"}

# Load the XSL stylesheet to transform the XML file to CSV
xslt_tree = ET.parse("<setpathto>/KiCad/6.0/plugins/bom2grouped_csv_jlcpcb.xsl")
transform = ET.XSLT(xslt_tree)

# Apply the transformation to the XML file
csv_data = transform(root)

# Convert the XSLTResultTree object to a string and split into lines
csv_data_str = str(csv_data)
csv_lines = csv_data_str.splitlines()

# Write the transformed CSV data to file
with open(csv_path, "w", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=",", quotechar="'", quoting=csv.QUOTE_NONE)
for line in csv_lines:
writer.writerow(line.split(","))

# Example usage
generate_bom(xml_path, csv_path)

and set the commandline to:

python "<setpathto>\KiCad\6.0\plugins\bom2grouped_csv_jlcpcb.py" "%I" "%O.csv"

1 Like

You probably want to edit your paste of your Python program to use the code tag, otherwise this forum’s markup processing turns your comments into bold paragraph headings.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.