Home > database >  XPath to return part of a string
XPath to return part of a string

Time:09-20

hope someone can advise me here with my XPath query. I want to extract and display part of a string but the result that I am getting at the moment is returning the full string. I want to be able to get two results like the ones below.

<airline>
    <flight-number flight_id="flt-888712-departure-date-arrival-arrival-date-0101">01</flight-number>
    <flight-number flight_id="flt-888712-departure-date-arrival-arrival-date-0102">02</flight-number>
</airline>

This is the xml file flights.xml that I am working with.

<airline>
    <flight-number flight_id="flt-888712-departure-date-arrival-date-0102">01–02</flight-number>
</airline>

For example, I just get 01-02 as my result when I try this XPath query below but more needs to be done to get what I stated above. I want the strings for 01 and 02 to be returned separately. /airline/flight-number/child::text()

Can someone advise me on how to achieve the results with XPath for what I am trying to do?

CodePudding user response:

Using xmlstarlet and bash:

alias xml=xmlstarlet
f1=$( xml sel -t -m 'airline/flight-number' -v 'substring-before(., "-")' infile.xml)
f2=$( xml sel -t -m 'airline/flight-number' -v 'substring-after (., "-")' infile.xml)
fid=$(xml sel -t -m 'airline/flight-number/@flight_id'               \
                 -v 'substring(., 1, string-length(.)-2)' infile.xml)

xml ed -d airline/flight-number infile.xml                           |
xml ed -s airline -type elem -n flight-number -v $f1                 |
xml ed -s airline -type elem -n flight-number -v $f2                 |
xml ed -a 'airline/flight-number[1]' -t attr -n flight_id -v $fid$f1 |
xml ed -a 'airline/flight-number[2]' -t attr -n flight_id -v $fid$f2

Output:

<?xml version="1.0"?>
<airline>
  <flight-number flight_id="flt-888712-departure-date-arrival-date-0101">01</flight-number>
  <flight-number flight_id="flt-888712-departure-date-arrival-date-0102">02</flight-number>
</airline>
  • Related