Question

I have an XML file which I'm trying to transform into something more like CSV format (well some format which puts the output on a single line with a common delimiter), but I can't quite get the syntax right, can anyone help please? The XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<TranslationTable>
        <Translation Key="Document1" RelativePath="/home/path1">
                <Title>Doc1</Title>
                <Description>First document</Description>
        </Translation>
        <Translation Key="Document2" RelativePath="/home/path2">
                <Title>Doc2</Title>
                <Description>Second document</Description>
        </Translation>
</TranslationTable>

And my command at present is this

xmlstarlet sel -t -m //Translation -v @Key -o "|" -v @RelativePath -n /root/XML/file.xml
Document1|/home/path1
Document2|/home/path2

But what I'd like is to also include the contents of the Title field, so that the output is like this

Document1|/home/path1|Doc1
Document2|/home/path2|Doc2

Can anyone help please? Thanks.

Was it helpful?

Solution

It's XPath:

xmlstarlet sel -t -m //Translation -v @Key -o "|" -v @RelativePath  -v 'Title/text()' -n /root/XML/file.xml
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top