使用 Javascript DOM 決議器從 WMS GetCapabilities請求的 XML response.data 中提取層串列我正在嘗試實作一個 xpath,它將采用 XML 回應示例并回傳一個包含鍵值對的物件葉層標簽,包括名稱、標題、摘要、維度:[Dimension_time、Dimension_reference_time]、樣式:[樣式名稱、標題和 URL] 所以對于上面的鏈接:
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 has the exact solution I was looking for and I think I understand better how SaxonJS works thanks to him. People like him are the backbone of programming and I hope I get to help others like he does one day.
uj5u.com熱心網友回復:
使用 XPath時,將選擇一個Layer不包含另一個的(暫時忽略名稱空間)。LayerLayer[not(.//Layer)]
使用 Saxon-JS 和 XPath 3.1,要在 JavaScript 端擁有 JavaScript 物件和陣列,您可以使用 XPath 3.1 映射 ( https://www.w3.org/TR/xpath-31/#id-maps ) 和陣列 ( https://www.w3.org/TR/xpath-31/#id-arrays):
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>
依賴 XPath 3.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 } });
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/442384.html
標籤:xml xslt xpath xml-parsing saxon
上一篇:C#決議XML并將其存盤到類中
