简体   繁体   English

将特定的XML解析为CSV格式

[英]Parse specific XML to CSV format

How can I, using some bash/shell script, transform this input 我如何使用一些bash / shell脚本转换此输入

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<runJobReturn xmlns="http://xml.org" xmlns:ns1="http://xml.org" xsi:type="ns1:runJobReturn">
    <ns1:item xsi:type="ns1:ArrayOf_xsd_string">
        <ns1:item xsi:type="xsd:string">15-02-2013</ns1:item>
        <ns1:item xsi:type="xsd:string">Benjamin</ns1:item>
        <ns1:item xsi:type="xsd:string">MASSY</ns1:item>
    </ns1:item>
    <ns1:item xsi:type="ns1:ArrayOf_xsd_string">
        <ns1:item xsi:type="xsd:string">15-02-2013</ns1:item>
        <ns1:item xsi:type="xsd:string">Ronald</ns1:item>
        <ns1:item xsi:type="xsd:string">MASSY</ns1:item>
    </ns1:item>
    <ns1:item xsi:type="ns1:ArrayOf_xsd_string">
        <ns1:item xsi:type="xsd:string">15-02-2013</ns1:item>
        <ns1:item xsi:type="xsd:string">Zachary</ns1:item>
        <ns1:item xsi:type="xsd:string">MASSY</ns1:item>
    </ns1:item>
    <ns1:item xsi:type="ns1:ArrayOf_xsd_string">
        <ns1:item xsi:type="xsd:string">12</ns1:item>
        <ns1:item xsi:type="xsd:string">13</ns1:item>
    </ns1:item>
    <ns1:item xsi:type="ns1:ArrayOf_xsd_string">
        <ns1:item xsi:type="xsd:string">12</ns1:item>
        <ns1:item xsi:type="xsd:string">13</ns1:item>
    </ns1:item>
</runJobReturn>
</soapenv:Body>

To this output: 到此输出:

15-02-2013|Benjamin|MASSY
15-02-2013|Ronald|MASSY
15-02-2013|Zachary|MASSY
12|13
12|13

Input is from curl. 输入来自curl。 I've tried to use sed : echo $INP | 我尝试使用sed:echo $ INP | tr -d "\\n" | tr -d“ \\ n” | sed -e 's/<[^>]*>/\\n/g' but in output remains multiply new lines between values sed -e's / <[^>] *> / \\ n / g',但在输出中仍在值之间乘以新行

You really shouldn't use regex to parse XML . 您确实不应该使用正则表达式来解析XML It's just as easy to run XSLT in bash. 在bash中运行XSLT一样容易。

I would recommend running either running the Java version of Saxon-HE from the command line (XSLT 2.0) or running XMLStarlet (XSLT 1.0). 我建议从命令行运行Java版本的Saxon-HE (XSLT 2.0)或运行XMLStarlet (XSLT 1.0)。

Examples: 例子:

XSLT 2.0 (Saxon) XSLT 2.0 (撒克逊人)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://xml.org">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="ns1:runJobReturn/ns1:item">
        <xsl:value-of select="ns1:item" separator="|"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

XSLT 1.0 (XMLStarlet, Saxon, Xalan, etc.) XSLT 1.0 (XMLStarlet,Saxon,Xalan等)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://xml.org">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="ns1:runJobReturn/ns1:item">
        <xsl:apply-templates select="ns1:item"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

    <xsl:template match="ns1:item">
        <xsl:if test="not(position()=1)">
            <xsl:text>|</xsl:text>
        </xsl:if>
        <xsl:value-of select="."/>
    </xsl:template>

</xsl:stylesheet>

Either one of these stylesheets, applied to your input XML, will produce the output you wanted: 将这些样式表之一应用于您的输入XML,将产生所需的输出:

15-02-2013|Benjamin|MASSY
15-02-2013|Ronald|MASSY
15-02-2013|Zachary|MASSY
12|13
12|13

这是一个快速的awk单线:

echo $INP |awk -F '[<>]' '$2 ~ "xsd:string" {row = row "|" $3} $2 == "/ns1:item" {print substr(row, 2) ; row = ""}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM