Edit

WMS GetFeatureInfo (Layers)

All features:
Hotel features:
Restaurant features:

Shows how to fetch features per layer name in a single WMS GetFeatureInfo request

Demonstrates the use of the layers option in the ol/format/WMSGetFeatureInfo format object, which allows features returned by a single WMS GetFeatureInfo request that asks for more than one layer to be read by layer name.

main.js
import 'ol/ol.css';
import WMSGetFeatureInfo from 'ol/format/WMSGetFeatureInfo';

fetch('data/wmsgetfeatureinfo/osm-restaurant-hotel.xml')
  .then(function (response) {
    return response.text();
  })
  .then(function (response) {
    // this is the standard way to read the features
    const allFeatures = new WMSGetFeatureInfo().readFeatures(response);
    document.getElementById('all').innerText = allFeatures.length.toString();

    // when specifying the 'layers' options, only the features of those
    // layers are returned by the format
    const hotelFeatures = new WMSGetFeatureInfo({
      layers: ['hotel'],
    }).readFeatures(response);
    document.getElementById('hotel').innerText =
      hotelFeatures.length.toString();

    const restaurantFeatures = new WMSGetFeatureInfo({
      layers: ['restaurant'],
    }).readFeatures(response);
    document.getElementById('restaurant').innerText =
      restaurantFeatures.length.toString();
  });
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>WMS GetFeatureInfo (Layers)</title>
    <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
    <script src="https://unpkg.com/elm-pep@1.0.6/dist/elm-pep.js"></script>
    <!-- The lines below are only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,TextDecoder"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/3.18.3/minified.js"></script>
    <style>
      .map {
        width: 100%;
        height:400px;
      }
    </style>
  </head>
  <body>
    <table id="info">
      <tr>
        <td>All features:</td>
        <td id="all"></td>
      </tr>
      <tr>
        <td>Hotel features:</td>
        <td id="hotel"></td>
      </tr>
      <tr>
        <td>Restaurant features:</td>
        <td id="restaurant"></td>
      </tr>
    </table>
    <script src="main.js"></script>
  </body>
</html>
package.json
{
  "name": "getfeatureinfo-layers",
  "dependencies": {
    "ol": "6.13.1-dev"
  },
  "devDependencies": {
    "parcel": "^2.0.0"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --public-url . index.html"
  }
}