【发布时间】:2019-12-11 16:37:36
【问题描述】:
我想将下面的 XML 转换为 CSV。尽管它在 XML 文件中有标题行信息。
<?xml version="1.0" encoding="UTF-8"?>
<myfile>
<updatetime>2019-07-30 08:30:30</updatetime>
<process code="PRS1234" name="PROCESS1234" />
<equipment code="EQP1234" name="EQUIPMENT1234" />
<product type="equipment" planned="300" time="36000" cycletime="20" />
<shift code="1" timestart="2019-07-30 02:00:00">
<order index="1" goodproduct="500" defectproduct="5" time="2019-07-30 02:00:00" />
<order index="2" goodproduct="980" defectproduct="7" time="2019-07-30 03:00:00" />
<order index="3" goodproduct="1200" defectproduct="12" time="2019-07-30 04:00:00" />
<order index="4" goodproduct="1800" defectproduct="15" time="2019-07-30 05:00:00" />
<order index="5" goodproduct="2500" defectproduct="15" time="2019-07-30 06:00:00" />
<shift>
<shift code="2" timestart="2019-07-30 07:00:00">
<order index="1" goodproduct="600" defectproduct="5" time="2019-07-30 07:00:00" />
<order index="2" goodproduct="980" defectproduct="7" time="2019-07-30 08:00:00" />
<order index="3" goodproduct="1500" defectproduct="8" time="2019-07-30 09:00:00" />
<order index="4" goodproduct="1700" defectproduct="11" time="2019-07-30 10:00:00" />
<order index="5" goodproduct="3000" defectproduct="15" time="2019-07-30 11:00:00" />
</shift>
</myfile>
我可以获得所需节点的值。这就是我所做的。
[xml]$inputFile = Get-Content "Q:\XML\FileComplex.xml"
$inputFile.myfile.product | Select-Object -Property type,planned,time,cycletime | ConvertTo-Csv -NoTypeInformation -Delimiter ";" | Set-Content -Path "Q:\XML\FileComplex.csv" -Encoding UTF8 "
我想要实现的是将所有需要的信息组合在一起,并将其作为 CSV 文件的一条记录,就像这样
updatetime | code(process) | name(process) | code(equipment) | name(equipment) | type(product) | planned(product) | time(product) | cycletime(product) | goodproduct(shift(code) is 1 and index is max) | defectproduct(shift(code) is 1 and index is max) | goodproduct(shift(code) is 2 and index is max) | defectproduct((shift(code) is 2 where index is max)
2019-07-30 08:30:30 | PRS1234 | PROCESS1234 | EQP1234 | EQUIPMENT1234 | equipment | 300 | 36000 | 20 | 2500 | 15 | 3000 | 15
非常感谢您的支持!!
提前致谢 娜塔莎
【问题讨论】:
-
请发布您的实际 XML 示例,或修复当前示例中的错误,使其真正可加载。
-
抱歉,我修复了它们@Tomalak
标签: xml powershell csv