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

时间:2021-05-24 16:05:23

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.

我对标签 之间的位感兴趣。而且,当标签 出现时,应该启动新的行。

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!

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

2 个解决方案

#1


2  

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样式表和xsltproc命令。如果它只是无条件地将所有数据转换为从单元格标记中使用逗号分隔值的行,那么它是一个相对简单的样式表。

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

快速搜索就会得到这样的结果:使用XSLT从XML到CSV,并对您的XML进行一些修改,它应该做您需要做的事情。

#2


0  

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.

这似乎是一种痛苦的生活方式。

#1


2  

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样式表和xsltproc命令。如果它只是无条件地将所有数据转换为从单元格标记中使用逗号分隔值的行,那么它是一个相对简单的样式表。

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

快速搜索就会得到这样的结果:使用XSLT从XML到CSV,并对您的XML进行一些修改,它应该做您需要做的事情。

#2


0  

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.

这似乎是一种痛苦的生活方式。