首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析WMS XML响应并返回包含键值对的对象。

解析WMS XML响应并返回包含键值对的对象。
EN

Stack Overflow用户
提问于 2022-03-10 19:24:39
回答 1查看 61关注 0票数 0

使用Javascript DOM解析器从WMS GetCapabilities请求的XML GetCapabilities提取层列表开始,我试图实现一个xpath,它将接受XML响应示例,并返回一个对象,该对象包含叶层标记的键值对,包括名称、标题、摘要、维度: Dimension_time、Dimension_reference_time、样式:样式、标题和URL,因此上面的链接是这样的:

代码语言:javascript
复制
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能够做到这一点,它将为我消除一个依赖项。

代码语言:javascript
复制
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);

编辑:谢谢马丁·霍宁先生

Honnen先生有我正在寻找的确切解决方案,我想我能更好地理解SaxonJS是如何工作的,这要感谢他。像他这样的人是编程的中坚力量,我希望有一天我能像他那样帮助别人。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-10 21:07:58

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

使用Saxon和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):

代码语言:javascript
复制
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);
代码语言:javascript
复制
<script src="https://www.saxonica.com/saxon-js/documentation2/SaxonJS/SaxonJS2.rt.js"></script>

依赖XPath 3.1地图的唯一缺点是它们是无序的,所以最终的结果可能是

代码语言:javascript
复制
{
  "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

代码语言:javascript
复制
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 } });

      })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71429705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档