在shell脚本中将xml文件转换为csv? [英] Convert xml file to csv in shell script?
问题描述
我正在尝试将xml文件转换为csv文件.我有一个这样的输入xml文件:
I'm trying to convert a xml file to a csv file. I have an input xml file like this:
<Row>
<Cell>
<Data Type="String" >START</Data>
</Cell>
<Cell>
<Data Type="DateTime" >2013-01-15T21:30:42</Data>
</Cell>
<Cell>
<Data Type="String" ></Data>
</Cell>
<Cell>
<Data Type="String" >Start 'suite8'</Data>
</Cell>
<Cell>
<Data Type="String" >Test 'suite8' started</Data>
</Cell>
<Cell>
<Data Type="String" ></Data>
</Cell>
</Row>
<Row/>
<Row>
<Cell>
<Data Type="String" >START_TEST_CASE</Data>
</Cell>
<Cell>
<Data Type="DateTime" >2013-01-15T21:30:42</Data>
</Cell>
<Cell>
<Data Type="String" ></Data>
</Cell>
<Cell>
<Data Type="String" >Start 'case1'</Data>
</Cell>
<Cell>
<Data Type="String" >Test Case 'case1' started</Data>
</Cell>
<Cell>
<Data Type="String" >case1</Data>
</Cell>
</Row>
我对标签<Data Type="String" >
和</Data>
之间的位感兴趣.另外,当标记<Row>
出现时,应该开始新的一行.
I'm interested in the bits between the tags <Data Type="String" >
and </Data>
. Also, a new line should be started when the tag <Row>
appears.
我想要的输出csv文件应如下所示:
The output csv file I want should look like this:
START,2013-01-15T21:30:42,,Test 'suite8' started
START_TEST_CASE,2013-01-15T21:30:42,,Start 'case1',Test Case 'case1' started,case1
我希望这已经足够清楚了,非常感谢您的帮助:)谢谢!
I hope this is clear enough, any help is greatly appreciated :) Thanks!
推荐答案
看看xslt样式表和xsltproc
命令.如果只是将所有数据无条件地转换为带有单元格标记中用逗号分隔的值的行,那么它就是一个相对简单的样式表.
Take a look at xslt stylesheets and the xsltproc
command. If it is just converting unconditionally all data to rows with comma separated values from the cell tags it's a relatively simple stylesheet.
快速搜索得出以下结果:使用XSLT将XML转换为CSV 通过对xml进行一些修改,它应该可以满足您的需求.
A quick search yielded this: XML to CSV Using XSLT With a few adaptations to your xml it should do what you need.
这篇关于在shell脚本中将xml文件转换为csv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!