分析WMS XML响应并返回一个包含键值对的对象



使用Javascript DOM Parser从WMS GetCapabilities请求的XML响应数据中提取层列表我正在尝试实现一个xpath,它将以XML响应为例,并返回一个对象,该对象包含叶层标记的键值对,包括Name、Title、Abstract、Dimension:[Dimension_time、Dimension_reference_time],样式:〔样式名称,标题和网址〕所以上面的链接:

Name: 'CGSL.ETA_ICEC',
Title: 'CGSL.ETA.ICEC - Fraction de glace',
Abstract: 'Le Système régional de prévision déterministe (SRPD) procède à ...',
Dimension: {
Dimension_time: '2022-03-09T13:00:00Z/2022-03-11T12:00:00Z/PT1H',
Dimension_ref_time: '2022-03-08T06:00:00Z/2022-03-09T12:00:00Z/PT6H'
},
Style: [
{
Name: 'SEA_ICECONC-LINEAR',
Title: 'SEA_ICECONC-LINEAR',
LegendWidth: 82,
LegendHeight: 155,
LegendURL: 'https://geo.weather.gc.ca/geomet?lang=fr&version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=CGSL.ETA_ICEC&format=image/png&STYLE=SEA_ICECONC-LINEAR'
},
{
Name: 'SEA_ICECONC',
Title: 'SEA_ICECONC',
LegendWidth: 82,
LegendHeight: 155,
LegendURL: 'https://geo.weather.gc.ca/geomet?lang=fr&version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=CGSL.ETA_ICEC&format=image/png&STYLE=SEA_ICECONC'
}
]

我的应用程序在浏览器中运行,我可以访问SaxonJS,但如果本地DomParser可以做到这一点,它将为我消除依赖。

axios.get('https://geo.weather.gc.ca/geomet/?service=WMS&version=1.3.0&request=GetCapabilities&LANG=en&LAYER=CGSL.ETA_ICEC')
.then((response) => {
const xslt =`XSLT`
const jsonResult = SaxonJS.XPath.evaluate(`
transform(
map { 
'source-node' : parse-xml($xml), 
'stylesheet-text' : $xslt, 
'delivery-format' : 'raw' 
}
)?output`,
[],
{ 'params': { 'xml': response.data, 'xslt': xslt }})
console.log(tempObject);

编辑:谢谢Martin Honnen先生

Mr。Honnen有我一直在寻找的确切解决方案,我想多亏了他,我才能更好地理解SaxonJS的工作原理。像他这样的人是编程的中坚力量,我希望有一天我能像他一样帮助别人。

使用XPath,Layer[not(.//Layer)]将选择不包含另一个LayerLayer(暂时忽略命名空间(。

使用SaxonJS和XPath3.1,要在JavaScript端拥有JavaScript对象和数组,可以使用XPath3.1映射(https://www.w3.org/TR/xpath-31/#id-地图(和阵列(https://www.w3.org/TR/xpath-31/#id-阵列(:

const result = SaxonJS.XPath.evaluate(`doc($url)//Layer[not(.//Layer)]!map {
'Name' : string(Name),
'Title' : string(Title),
'Abstract' : string(Abstract),
'Dimension' : map {
'Dimension_time' : string(Dimension[@name = 'time']),
'Dimension_ref_time' : string(Dimension[@name = 'reference_time'])
},
'Style' : array { Style !
map {
'Name' : string(Name),
'Title' : string(Title),
'LegendWith' : string(LegendURL/@width),
'LegendHeight' : string(LegendURL/@height),
'LegendURL' : string(LegendURL/OnlineResource/@xlink:href)
}
}
}`, null, { xpathDefaultNamespace : 'http://www.opengis.net/wms', namespaceContext : { xlink : 'http://www.w3.org/1999/xlink' }, params : { url : 'https://geo.weather.gc.ca/geomet/?service=WMS&version=1.3.0&request=GetCapabilities&LANG=en&LAYER=CGSL.ETA_ICEC' } });
console.log(result);


<script src="https://www.saxonica.com/saxon-js/documentation2/SaxonJS/SaxonJS2.rt.js"></script>

依赖XPath3.1映射的唯一缺点是它们是无序的,所以最终的结果可能看起来像

{
"Abstract": "The Regional Deterministic Prediction System (RDPS) carries out physics calculations to arrive at deterministic predictions of atmospheric elements from the current day out to 84 hours into the future. Atmospheric elements include temperature, precipitation, cloud cover, wind speed and direction, humidity and others. This product contains raw numerical results of these calculations. Geographical coverage includes Canada and the United States. Data is available at horizontal resolution of about 10 km up to 33 vertical levels. Predictions are performed four times a day.",
"Dimension": {
"Dimension_time": "2022-03-10T13:00:00Z/2022-03-12T12:00:00Z/PT1H",
"Dimension_ref_time": "2022-03-09T06:00:00Z/2022-03-10T12:00:00Z/PT6H"
},
"Name": "CGSL.ETA_ICEC",
"Title": "CGSL.ETA.ICEC - Ice cover fraction",
"Style": [
{
"LegendWith": "82",
"LegendURL": "https://geo.weather.gc.ca/geomet?version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=CGSL.ETA_ICEC&format=image/png&STYLE=SEA_ICECONC-LINEAR",
"LegendHeight": "155",
"Name": "SEA_ICECONC-LINEAR",
"Title": "SEA_ICECONC-LINEAR"
},
{
"LegendWith": "82",
"LegendURL": "https://geo.weather.gc.ca/geomet?version=1.3.0&service=WMS&request=GetLegendGraphic&sld_version=1.1.0&layer=CGSL.ETA_ICEC&format=image/png&STYLE=SEA_ICECONC",
"LegendHeight": "155",
"Name": "SEA_ICECONC",
"Title": "SEA_ICECONC"
}
]
}

即,对象可能具有与所需结果不同的顺序的属性。

如果您将XML作为字符串,作为response.data,请使用parse-xml,例如

axios.get('https://geo.weather.gc.ca/geomet/?service=WMS&version=1.3.0&request=GetCapabilities&LANG=en&LAYER=CGSL.ETA_ICEC')
.then((response) => {
const result = SaxonJS.XPath.evaluate(`parse-xml($xml)//Layer[not(.//Layer)]!map {
'Name' : string(Name),
'Title' : string(Title),
'Abstract' : string(Abstract),
'Dimension' : map {
'Dimension_time' : string(Dimension[@name = 'time']),
'Dimension_ref_time' : string(Dimension[@name = 'reference_time'])
},
'Style' : array { Style !
map {
'Name' : string(Name),
'Title' : string(Title),
'LegendWith' : string(LegendURL/@width),
'LegendHeight' : string(LegendURL/@height),
'LegendURL' : string(LegendURL/OnlineResource/@xlink:href)
}
}
}`, null, { xpathDefaultNamespace : 'http://www.opengis.net/wms', namespaceContext : { xlink : 'http://www.w3.org/1999/xlink' }, params : { xml: response.data } });
})

相关内容

最新更新