Jump to the navigation menu

Parsing XML with xq

Parsing XML with xq

Installation

nix-shell -p yq - this also includes xq.

Sample code

<?xml version="1.0" encoding="utf-8"?>
<StationList>
  <Station>
    <StationCode>CDF</StationCode>
    <Name>Cardiff Central</Name>
    <StationAlerts>
      <AlertText><![CDATA[<p>The Riverside car park near platform 0 at Cardiff Central will be closed until 12:00 on Thursday 2 April.</p><p>Please use the Penarth Road car park or Cardiff Central Quay multi-storey during this time.</p>]]></AlertText>
    </StationAlerts>
  </Station>
  <Station>
    <StationCode>NWP</StationCode>
    <Name>Newport (South Wales)</Name>
  </Station>
  <Station>
    <StationCode>SWA</StationCode>
    <Name>Swansea</Name>
  </Station>
</StationList>

Example commands

Getting all results

cat sample.xml | xq '.StationList.Station[]'
{
  "StationCode": "CDF",
  "Name": "Cardiff Central",
  "StationAlerts": {
    "AlertText": "<p>The Riverside car park near platform 0 at Cardiff Central will be closed until 12:00 on Thursday 2 April.</p><p>Please use the Penarth Road car park or Cardiff Central Quay multi-storey during this time.</p>"
  }
}
{
  "StationCode": "NWP",
  "Name": "Newport (South Wales)"
}
{
  "StationCode": "SWA",
  "Name": "Swansea"
}

Getting all results as XML

cat sample.xml | xq -x '.StationList.Station[]'
<StationCode>CDF</StationCode>
<Name>Cardiff Central</Name>
<StationAlerts>
  <AlertText>&lt;p&gt;The Riverside car park near platform 0 at Cardiff Central will be closed until 12:00 on Thursday 2 April.&lt;/p&gt;&lt;p&gt;Please use the Penarth Road car park or Cardiff Central Quay multi-storey during this time.&lt;/p&gt;</AlertText>
</StationAlerts>

<StationCode>NWP</StationCode><Name>Newport (South Wales)</Name>

<StationCode>SWA</StationCode><Name>Swansea</Name>

-x returns the value as XML instead of JSON.

Plucking values by key

cat sample.xml | xq  '.StationList.Station[] | {StationCode}'
{
  "StationCode": "CDF"
}
{
  "StationCode": "NWP"
}
{
  "StationCode": "SWA"
}

Plucking multiple values

cat sample.xml | xq  '.StationList.Station[] | {StationCode,StationAlerts}'
{
  "StationCode": "CDF",
  "StationAlerts": {
    "AlertText": "<p>The Riverside car park near platform 0 at Cardiff Central will be closed until 12:00 on Thursday 2 April.</p><p>Please use the Penarth Road car park or Cardiff Central Quay multi-storey during this time.</p>"
  }
}
{
  "StationCode": "NWP",
  "StationAlerts": null
}
{
  "StationCode": "SWA",
  "StationAlerts": null
}

Filtering by field values

cat sample.xml | xq  '.StationList.Station[] | select(.StationCode == "NWP") | {StationCode,StationAlerts}'
{
  "StationCode": "NWP",
  "StationAlerts": null
}
cat sample.xml | xq  '.StationList.Station[] | select(.StationCode == "CDF") | {StationCode,StationAlerts}'
{
  "StationCode": "CDF",
  "StationAlerts": {
    "AlertText": "<p>The Riverside car park near platform 0 at Cardiff Central will be closed until 12:00 on Thursday 2 April.</p><p>Please use the Penarth Road car park or Cardiff Central Quay multi-storey during this time.</p>"
  }
}