Hi I need make some text manipulation of this part of xml. Deleting some tags is no problem. I need before that rename car ID to CAR_ID and move inside TRIP tags.
ie: MLStarlet Toolkit ?
xmlstarlet somevariable
Original
<car>
<id>155028827</id>
<trip>
<id>1</id>
<date>1.1.1970</date>
</trip>
<trip>
<id>2</id>
<date>1.1.1970</date>
</trip>
</car>
Expection result
<trip>
<car_id>155028827</id>
<id>1</id>
<date>1.1.1970</date>
</trip>
<trip>
<car_id>155028827</id>
<id>2</id>
<date>1.1.1970</date>
</trip>
I'd say
This falls into two parts:
and
The first is an
xmlstarlet edcommand, which means that XML goes in, is edited, and edited XML goes out. The edits arewhich inserts a
car_idbefore the first descendant of every/car/tripnode, andwhich sets the value of all
/car/trip/car_idnodes to the text inside theidsubnode of theircarancestor node. This alone produceswhich is then piped through
This selects (and prints) the
/car/tripnodes of this XML data, producingYou could, if the formatting annoys you, use
to preserve the whitespaces between the tags (and get more readably formatted output); with this change, the output is
...with two more blank lines at the top; they're the newlines before and after the
/car/idnode. Unfortunately, the output data is no longer valid XML, so we can't just pipe it through an XML pretty-printer (which is what I'd really like to do). Since my suspicion is that this will be embedded in further XML (so that it can be properly parsed), in the event that formatting is important, my suggestion is to embed this first and pipe the whole XML through a pretty-printer afterwards.