Linux 在shell脚本中将xml文件转换为csv?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14368347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:41:58  来源:igfitidea点击:

Convert xml file to csv in shell script?

xmllinuxshellcsv

提问by Babycece

I'm trying to convert a xml file to a csv file. I have an input xml file like this:

我正在尝试将 xml 文件转换为 csv 文件。我有一个像这样的输入xml文件:

<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>

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.

我对标签<Data Type="String" ></Data>. 此外,当标签<Row>出现时,应该开始一个新行。

The output csv file I want should look like this:

我想要的输出 csv 文件应该是这样的:

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!

我希望这足够清楚,非常感谢任何帮助:) 谢谢!

回答by Slartibartfast

Parsing XML with Bash has been addressed here before:

使用 Bash 解析 XML 之前已在此处解决:

How to parse XML in Bash?

如何在 Bash 中解析 XML?

That said it seems like a painful way to live.

也就是说,这似乎是一种痛苦的生活方式。

回答by Bernhard

Take a look at xslt stylesheets and the xsltproccommand. 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 样式表和xsltproc命令。如果它只是无条件地将所有数据转换为单元格标签中带有逗号分隔值的行,那么它是一个相对简单的样式表。

A quick search yielded this: XML to CSV Using XSLTWith a few adaptations to your xml it should do what you need.

快速搜索结果如下:XML to CSV Using XSLT通过对您的 xml 进行一些调整,它应该可以满足您的需求。