我写了一个查询xmlstarlet sel -T -t -m badges/row -v "concat(@row, ',')" -n Badges.xml
,输出重复如下:
NaNNaNNaN...
Badges.xml 文件中可能的行数。我正在关注这个:https://www.joyofdata.de/blog/transforming-xml-document-into-csv-using-xmlstarlet/将 xml 格式的数据转换为 CSV。有人明白这是怎么回事吗?
编辑,以下是 XML 文件的一些示例行:
<?xml version="1.0" encoding="utf-8"?>
<badges>
<row Id="1" UserId="1" Name="Autobiographer" Date="2016-08-02T15:56:07.133"
Class="3" TagBased="False" />
<row Id="2" UserId="8" Name="Autobiographer" Date="2016-08-02T15:56:07.133"
Class="3" TagBased="False" />
<row Id="3" UserId="9" Name="Autobiographer" Date="2016-08-02T15:56:07.133"
Class="3" TagBased="False" />
<row Id="4" UserId="19" Name="Autobiographer" Date="2016-08-
02T15:56:07.133" Class="3" TagBased="False" />
<row Id="5" UserId="27" Name="Autobiographer" Date="2016-08-
02T15:56:07.133" Class="3" TagBased="False" />
答案1
在XPATH 中@
指向一个属性名称。要根据每个<row>
节点的属性值生成 csv 记录,请使用以下方法:
xmlstarlet sel -T -t -m 'badges/row' -v \
'concat(@Id, ",", @UserId, ",", @Name, ",", translate(@Date," ",""),
",", @Class, ",", @TagBased)' -n Badges.xml
输出:
1,1,Autobiographer,2016-08-02T15:56:07.133,3,False
2,8,Autobiographer,2016-08-02T15:56:07.133,3,False
3,9,Autobiographer,2016-08-02T15:56:07.133,3,False
4,19,Autobiographer,2016-08-02T15:56:07.133,3,False
5,27,Autobiographer,2016-08-02T15:56:07.133,3,False