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 ed
command, which means that XML goes in, is edited, and edited XML goes out. The edits arewhich inserts a
car_id
before the first descendant of every/car/trip
node, andwhich sets the value of all
/car/trip/car_id
nodes to the text inside theid
subnode of theircar
ancestor node. This alone produceswhich is then piped through
This selects (and prints) the
/car/trip
nodes 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/id
node. 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.