WMS Capabilities
Runnable OpenLayers example: WMS Capabilities. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:OpenLayers WMS 能力文档
要点:ol.format.WMSCapabilities 解析 GetCapabilities XML,输出服务、图层与请求能力
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WMS 能力文档</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:10px;align-items:center;flex-wrap:wrap;font-size:12px;color:#58606e}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
.bar select,.bar input[type=range]{font-size:12px}
.bar .val{font-family:ui-monospace,Consolas,monospace;color:#1f5fd0;min-width:44px}
.bar .sep{width:1px;height:16px;background:#d8dce2}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
.log{flex:1 1 auto;margin:0;padding:12px;overflow:auto;background:#1d2530;color:#d6e2ee;
font:11px/1.55 ui-monospace,Consolas,monospace;white-space:pre-wrap}
</style>
</head>
<body>
<div class="bar">
<span>WMS 1.3.0 能力文档</span>
<span class="sep"></span>
<span id="info">解析中…</span>
</div>
<pre id="log" class="log"></pre>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('log').textContent = 'OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net';
return;
}
var xml = '<WMS_Capabilities version="1.3.0" ' +
'xmlns="http://www.opengis.net/wms" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink">' +
'<Service><Name>WMS</Name><Title>演示 WMS 服务</Title>' +
'<Abstract>页面内构造的能力文档</Abstract>' +
'<OnlineResource xlink:type="simple" xlink:href="https://demo.invalid/wms"/></Service>' +
'<Capability>' +
'<Request>' +
'<GetCapabilities><Format>text/xml</Format></GetCapabilities>' +
'<GetMap><Format>image/png</Format><Format>image/jpeg</Format>' +
'<DCPType><HTTP><Get><OnlineResource xlink:type="simple" ' +
'xlink:href="https://demo.invalid/wms"/></Get></HTTP></DCPType></GetMap>' +
'<GetFeatureInfo><Format>text/plain</Format></GetFeatureInfo>' +
'</Request>' +
'<Exception><Format>XML</Format></Exception>' +
'<Layer><Title>演示根图层</Title><CRS>EPSG:3857</CRS>' +
'<EX_GeographicBoundingBox>115.8 39.5 117.0 40.3</EX_GeographicBoundingBox>' +
'<Layer queryable="1"><Name>demo:districts</Name><Title>演示区县</Title>' +
'<CRS>EPSG:3857</CRS><Style><Name>default</Name><Title>默认样式</Title></Style>' +
'</Layer>' +
'<Layer queryable="1"><Name>demo:water</Name><Title>演示水域</Title>' +
'<CRS>EPSG:3857</CRS><Style><Name>default</Name><Title>默认样式</Title></Style>' +
'</Layer>' +
'</Layer>' +
'</Capability>' +
'</WMS_Capabilities>';
var result = new ol.format.WMSCapabilities().read(xml);
document.getElementById('log').textContent = JSON.stringify(result, null, 2);
var layers = 0;
function countLayers(node) {
if (!node) {
return;
}
if (node.Name) {
++layers;
}
(node.Layer || []).forEach(countLayers);
}
countLayers(result.Capability && result.Capability.Layer);
document.getElementById('info').textContent =
'版本 ' + result.version + ' · ' + (result.Service && result.Service.Title) +
' · 具名图层 ' + layers + ' 个';
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.