Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将特定XML解析为CSV格式_Xml_Bash - Fatal编程技术网

将特定XML解析为CSV格式

将特定XML解析为CSV格式,xml,bash,Xml,Bash,如何使用一些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=

如何使用一些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>
输入来自curl。我尝试使用sed: echo$INP | tr-d“\n”| sed-e's/]*>/\n/g”
但在输出中,仍然会在值之间乘以新行

这里有一个快速awk单线:

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

这里是一个快速awk单行程序:

echo $INP |awk -F '[<>]' '$2 ~ "xsd:string" {row = row "|" $3} $2 == "/ns1:item" {print substr(row, 2) ; row = ""}'
echo$INP | awk-F'[]''''$2~”xsd:string“{row=row”|“$3}$2==”/ns1:item“{print substr(row,2);row=”“}”
您真的不应该使用。在bash中运行XSLT同样容易

我建议运行Java版本的(XSLT2.0)或运行(XSLT1.0)

示例:

XSLT2.0(萨克森)

你真的不应该用。在bash中运行XSLT同样容易

我建议运行Java版本的(XSLT2.0)或运行(XSLT1.0)

示例:

XSLT2.0(萨克森)


不要使用regex/sed/awk处理xml。不要使用regex/sed/awk来处理xml。及
<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>
<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>
15-02-2013|Benjamin|MASSY
15-02-2013|Ronald|MASSY
15-02-2013|Zachary|MASSY
12|13
12|13