bash 我应该如何将 xml 转换为 csv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13317053/
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-09-18 03:45:14  来源:igfitidea点击:

how should I go about converting xml into csv

xmlbashxml-parsing

提问by lacrosse1991

I am trying to take some xml code, a sample being below:

我正在尝试使用一些 xml 代码,示例如下:

  <time_report>

 <project_ID>4</project_ID>

 <project_status>close</project_status>

 <client_ID>6001</client_ID>

     <time_record>

            <project_start_time>15:02:33</project_start_time>

            <project_end_time>15:07:44</project_end_time>

            <project_total_time>PT00H05M11S</project_total_time>

     </time_record>

 <employee_ID>10001</employee_ID>

 <employee_name>Mary Beth</employee_name>

 <date_created>2009-08-25</date_created>

</time_report>

and then output it so it is in the following format:

然后将其输出为以下格式:

project_id, project_status, client_id, project_start_time, project_end_time,  project_total_time, employee_ID, employee_name, date_created

4, close, 6001, 15:02:33, 15:07:44, PT00H05M11S, 10001, Mary Beth, 2009-08-25

I have been trying to use xmllint to do this, but have unfortunately not been able to make any progress, having said that I was wondering if anyone would have a suggestion as to what I should do? I would be doing this in a bash/shell environment. any help would be much appreciated, thanks!

我一直在尝试使用 xmllint 来做到这一点,但不幸的是未能取得任何进展,我想知道是否有人会对我应该做什么提出建议?我将在 bash/shell 环境中执行此操作。任何帮助将不胜感激,谢谢!

also forgot to mention that I can get the correct results if I open the xml file up in excel and then save as csv, just looking for a way to do it in linux

还忘了说,如果我在 excel 中打开 xml 文件然后另存为 csv,我可以获得正确的结果,只是在 linux 中寻找一种方法

    project_ID,project_status,client_ID,project_start_time,project_end_time,project_total_time,employee_ID,employee_name,date_created
4,close,6001,15:02:33,15:07:44,PT00H05M11S,10001,Mary Beth,8/25/2009
5,open,6003,12:00:00,12:45:00,PT00H45M00S,10003,Michelle,9/11/2009
2,close,6002,10:00:00,10:30:00,PT00H30M00S,10002,Joe,8/25/2009
2,open,6004,12:00:00,3:27:05,PT03H23M05S,10004,Mike,8/13/2009

回答by Alastair McCormack

xmlstarlet is a very powerful command line tool which lets you query XML or run XSLT translations. There's some XSLT XML->CSV examples floating around but the following one-liner gives you what you need:

xmlstarlet 是一个非常强大的命令行工具,它可以让您查询 XML 或运行 XSLT 翻译。有一些 XSLT XML->CSV 示例随处可见,但以下单行代码可为您提供所需的内容:

xmlstarlet sel -B -t -m "//time_reports/time_report" -n -m "*" -v . -o , input.xml

The only problem was that I needed to wrap <time_report>with a root level tag called <time_reports>

唯一的问题是我需要<time_report>用一个名为<time_reports>

回答by kpater87

To transform your XML to CSV (with e.g. xsltproc) you can use an XSL stylesheet like this:

要将您的 XML 转换为 CSV(例如使用 xsltproc),您可以使用这样的 XSL 样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/">
        <xsl:for-each select="//time_report[position()=1]/*">
            <xsl:if test="not(position()=1)">
                <xsl:text>,</xsl:text>
            </xsl:if>
            <xsl:value-of select="name()" />
        </xsl:for-each>
        <xsl:text>&#13;</xsl:text>
        <xsl:for-each select="//time_report">
            <xsl:for-each select="./*">
                <xsl:if test="not(position()=1)">
                    <xsl:text>,</xsl:text>
                </xsl:if>
                <xsl:value-of select="normalize-space(.)" />
            </xsl:for-each>
            <xsl:text>&#13;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

回答by BeniBela

You could also use my Xidel: (assuming you have no empty fields in your xml)

您也可以使用我的Xidel:(假设您的 xml 中没有空字段)

 xidel /tmp/test.xml -e '//time_report/string-join(.//text()[normalize-space(.)], ", ")'

standard XPath 2, no need to remember the names of different command line parameters...

标准XPath 2,无需记住不同命令行参数的名称...

or without that assumption:

或者没有这个假设:

 xidel /tmp/test.xml -e '//time_report/string-join(.//*[not(*)], ", ")'