Install xmlstarlet (download from http://xmlstar.sourceforge.net/ or on ubuntu sudo apt-get install xmlstarlet).
Let's look at a simple XML file (menu.xml):
<BREAKFAST_MENU>
<FOOD NAME="Belgian Waffles" PRICE="$5.95">
<DESCRIPTION>two of our famous Belgian Waffles with plenty of real maple syrup</DESCRIPTION>
<CALORIES>650</CALORIES>
</FOOD>
<FOOD NAME="Strawberry Belgian Waffles" PRICE="$7.95">
<DESCRIPTION>light Belgian waffles covered with strawberries and whipped cream</DESCRIPTION>
<CALORIES>900</CALORIES>
</FOOD>
</BREAKFAST_MENU>
Convert the XML to PYX:
xmlstarlet pyx menu.xml > menu.pyx
The new file will now look like this:
(BREAKFAST_MENU
-\n\t
(FOOD
ANAME Belgian Waffles
APRICE $5.95
-\n\t\t
(DESCRIPTION
-two of our famous Belgian Waffles with plenty of real maple syrup
)DESCRIPTION
-\n\t\t
(CALORIES
-650
)CALORIES
-\n\t
)FOOD
-\n\t
(FOOD
ANAME Strawberry Belgian Waffles
APRICE $7.95
-\n\t\t
(DESCRIPTION
-light Belgian waffles covered with strawberries and whipped cream
)DESCRIPTION
-\n\t\t
(CALORIES
-900
)CALORIES
-\n\t
)FOOD
-\n
)BREAKFAST_MENU
Now we need to convert the lines beginning with "(" and ")" to lower case, and the first word of the lines beginning with an "A" (except the first "A").
cat menu.pyx | awk '{if (/^\(/) print tolower($0); else if (/^\)/) print tolower($0); else if (/^A/) print substr($0, 1, 1) tolower(substr($0, 2, index($0, " ")-1)) substr($0, index($0, " ")+1); else print $0; }' > menu.tmp
We now have the converted PYX file and this can be convert back to XML
xmlstarlet p2x menu.tmp > menu.xml
That's it, we have the converted XML file:
<breakfast_menu>
<food name="Belgian Waffles" price="$5.95">
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food name="Strawberry Belgian Waffles" price="$7.95">
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>
CodeProject