-
Notifications
You must be signed in to change notification settings - Fork 1
/
bom2csv.xsl
109 lines (90 loc) · 5.2 KB
/
bom2csv.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!--XSL style sheet to convert EESCHEMA XML Partlist Format to CSV BOM Format
Copyright (C) 2013, Stefan Helmert
with later changes by Andrew Baxter 2016
GPL v2.
Functionality:
Generation of csv table with table head of all existing field names
and correct assigned cell entries
How to use this is explained in eeschema.pdf chapter 14. You enter a command line into the
netlist exporter using a new (custom) tab in the netlist export dialog. The command is
similar to
on Windows:
xsltproc -o "%O.csv" "C:\Program Files (x86)\KiCad\bin\plugins\bom2csv.xsl" "%I"
on Linux:
xsltproc -o "%O.csv" /usr/local/lib/kicad/plugins/bom2csv.xsl "%I"
Instead of "%O.csv" you can alternatively use "%O" if you will supply your own file extension when
prompted in the UI. The double quotes are there to account for the possibility of space(s)
in the filename.
-->
<!--
@package
Generate a Tab delimited list (csv file type).
Components are grouped by a set of unique fields, and sorted usefully.
Component references in the same group are aggregated, and a quantity field is added.
This assumes that you have fields 'Tolerance', 'Package', and 'Product code' as well as the standard fields. If you want to group on other custom fields, you'll need to edit this file accordingly.
-->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nl "
"> <!--new line CR, LF, or LF, your choice -->
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<!-- for table head and empty table fields-->
<xsl:key name="headentr" match="field" use="@name"/>
<xsl:key name="groupon" match="components/comp" use="concat(translate(@ref,'0123456789', ''), '|', value, '|', fields[@name = 'Tolerance'], '|', fields[@name = 'Package'], '|', fields[@name = 'Product code'])"/>
<!-- main part -->
<xsl:template match="/export">
<xsl:text>Reference Type, Quantity, Reference, Value, Footprint, Datasheet</xsl:text>
<!-- find all existing table head entries and list each one once -->
<xsl:for-each select="components/comp/fields/field[generate-id(.) = generate-id(key('headentr',@name)[1])]">
<xsl:text>, </xsl:text>
<xsl:value-of select="@name"/>
</xsl:for-each>
<xsl:text>&nl;</xsl:text>
<!-- all table entries -->
<xsl:apply-templates select="components/comp[generate-id(.) = generate-id(key('groupon', concat(translate(@ref,'0123456789', ''), '|', value, '|', fields[@name = 'Tolerance'], '|', fields[@name = 'Package'], '|', fields[@name = 'Product code'])))]">
<xsl:sort select="translate(@ref,'0123456789','')"/>
<xsl:sort select="translate(value,'0123456789.','')"/>
<xsl:sort select="translate(value,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_','')" data-type="number"/>
<xsl:sort select="fields/field[@name='Product code']"/>
</xsl:apply-templates>
</xsl:template>
<!-- the table entries -->
<xsl:template match="components/comp">
<xsl:text>"</xsl:text>
<xsl:value-of select="translate(@ref,'0123456789','')"/><xsl:text>","</xsl:text>
<xsl:value-of select="count(key('groupon', concat(translate(@ref,'0123456789', ''), '|', value, '|', fields[@name = 'Tolerance'], '|', fields[@name = 'Package'], '|', fields[@name = 'Product code'])))"/><xsl:text>","</xsl:text>
<xsl:for-each select="key('groupon', concat(translate(@ref,'0123456789', ''), '|', value, '|', fields[@name = 'Tolerance'], '|', fields[@name = 'Package'], '|', fields[@name = 'Product code']))">
<xsl:value-of select="@ref"/><xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:text>","</xsl:text>
<xsl:value-of select="value"/><xsl:text>","</xsl:text>
<xsl:value-of select="footprint"/><xsl:text>","</xsl:text>
<xsl:value-of select="datasheet"/>
<xsl:text>"</xsl:text>
<xsl:apply-templates select="fields"/>
<xsl:text>&nl;</xsl:text>
</xsl:template>
<!-- table entries with dynamic table head -->
<xsl:template match="fields">
<!-- remember current fields section -->
<xsl:variable name="fieldvar" select="field"/>
<!-- for all existing head entries -->
<xsl:for-each select="/export/components/comp/fields/field[generate-id(.) = generate-id(key('headentr',@name)[1])]">
<xsl:variable name="allnames" select="@name"/>
<xsl:text>,"</xsl:text>
<!-- for all field entries in the remembered fields section -->
<xsl:for-each select="$fieldvar">
<!-- only if this field entry exists in this fields section -->
<xsl:if test="@name=$allnames">
<!-- content of the field -->
<xsl:value-of select="."/>
</xsl:if>
<!--
If it does not exist, use an empty cell in output for this row.
Every non-blank entry is assigned to its proper column.
-->
</xsl:for-each>
<xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>