1

So, I need help trying to parse an XML response in Bash. Let's say my response is this. (The response is abridged, but only shows the info I need.)

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <submissions>
        <submission>
            <submission_id><![CDATA[90210]]></submission_id>
            <last_file_update_datetime><![CDATA[2017-06-18 02:47:14.39864+02]]></last_file_update_datetime>
        </submission>
        <submission>
            <submission_id><![CDATA[90211]]></submission_id>
            <last_file_update_datetime><![CDATA[2017-06-11 15:48:04.279135+02]]></last_file_update_datetime>
        </submission>
    </submissions>
</response>

I want to parse for each block in <submissions>, and export the data into an array in this format:

{submission_id}#{last_file_update_datetime}#1

As an example, the response should look like this when parsed:

90210#2017-06-18 02:47:14.39864+02#1
90211#2017-06-11 15:48:04.279135+02#1

How can I perform this in Bash?

2

1 Answer 1

0

I have found my in experience that problems that involve reformatting XML are best handled with XSLT. I don't know if xsltproc is on your box, but if it is, here is some code that will get you what you want:

$ xsltproc stylesheet.xsl input.xml


        90210#2017-06-18 02:47:14.39864+02
        90211#2017-06-11 15:48:04.279135+02


~
$ cat stylesheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:output method="text" />

<xsl:template match="//submission">
  <xsl:value-of select="submission_id"/>#<xsl:value-of select="last_file_update_datetime"/>
</xsl:template>

</xsl:stylesheet>


$ cat input.xml
<?xml version="1.0" encoding="UTF-8"?>
<response>
    <submissions>
        <submission>
            <submission_id><![CDATA[90210]]></submission_id>
            <last_file_update_datetime><![CDATA[2017-06-18 02:47:14.39864+02]]></last_file_update_datetime>
        </submission>
        <submission>
            <submission_id><![CDATA[90211]]></submission_id>
            <last_file_update_datetime><![CDATA[2017-06-11 15:48:04.279135+02]]></last_file_update_datetime>
        </submission>
    </submissions>
</response>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.