1

I have an xml file source.xml. I want to create an property file with key-value pairs from this xml file info.

<?xml version="1.0" ?>
<persons>
  <person>
    <name>John</name>
    <family-name>Smith</family-name>
  </person>
  <person>
    <name>Morka</name>
    <family-name>Ismincius</family-name>
  </person>
</persons>

Property file should look like

"John" = "Smith";
"Morka" = "Ismincius";

Anyone did similar kind of conversion using Unix Shell Script please suggest..

Can it be done simply using grep and sed command.

Suvasis
  • 1,451
  • 4
  • 24
  • 42

2 Answers2

1

this may help?

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//person">
"<xsl:value-of select="name"/>" = "<xsl:value-of select="family-name"/>"
</xsl:template>
</xsl:stylesheet>

save it as x.xslt and do

xsltproc x.xslt file.xml
Kent
  • 189,393
  • 32
  • 233
  • 301
  • I have tried all processes like xsltproc, saxon and eclipse plugin etc. But requirement is to perform this using Shell script. Do u have any idea? – Suvasis Sep 12 '13 at 14:32
1

Searched a lot and finally able to create properties file from xml using simple grep and sed unix commands.

Couple of links good enough to refer :

How can I replace a newline (\n) using sed?

put every alternate row in a column using some Unix commands

The script worked :

grep -e "<name>" -e "<family-name>" source.xml  | 
sed -e 's/<name>/"/' -e 's/<\/name>/"/' | 
sed -e 's/<family-name>/"/' -e 's/<\/family-name>/";/' |
sed 's/ //g' |
sed 'N;s/\n/ = /' > my.properties

Anyone having any other way to do the same using simple unix commands please suggest.

Community
  • 1
  • 1
Suvasis
  • 1,451
  • 4
  • 24
  • 42