微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XSLT WMS GetCapabilities 到 JSON

如何解决XSLT WMS GetCapabilities 到 JSON

我正在尝试将 GeoMet WMS XML response.data 的 GetCapabilities 转换为可在 v-treeview Vuetify 组件(例如 in this link)中使用的 JSON。

testfunc: function () {
      axios.get('https://geo.weather.gc.ca/geomet?lang=en&service=WMS&version=1.3.0&request=GetCapabilities').then((response) => {
        const xslt = `<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
  xmlns="http://www.w3.org/2005/xpath-functions"
  xmlns:mf="http://example.com/mf"
  expand-text="yes"
    version="3.0">
  
  <xsl:strip-space elements="*"/>

  <xsl:output method="json" build-tree="no"/>
  
  <xsl:template match="/Layer" priority="5">
    <xsl:map>
      <xsl:apply-templates/>
    </xsl:map>
  </xsl:template>
  
  <xsl:template match="*[not(*)]">
    <xsl:map-entry key="local-name()" select="data()"/>
  </xsl:template>
  
  <xsl:template match="Layer[1]">
    <xsl:map-entry key="'children'">
      <xsl:sequence select="array { mf:apply-templates(*) }"/>
    </xsl:map-entry>
  </xsl:template>
  
  <xsl:template match="Layer[position() > 1]"/>
  
  <xsl:function name="mf:apply-templates" as="item()*">
    <xsl:param name="elements" as="element(*)*"/>
    <xsl:apply-templates select="$elements"/>
  </xsl:function>
  
</xsl:stylesheet>`
        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(jsonResult)
      })
    }

我的测试函数返回所有信息,并没有真正按照我需要的方式解析 XML 响应,而且我是 XSLT 的新手。我需要的东西只返回 <Name> 标签<Title><Layer> 内层 HTML 及其子元素在名为 children 的数组中,如下所示:

{
title: 'Title Level 1'
name: 'Name Level 1'
children: [
    {
     title: 'Title Level 2'
     name: 'Name Level 2'
     children: [
         {
          title: 'Title Level 3-1'
          name: 'Name Level 3-1'
         },{
          title: 'Title Level 3-2'
          name: 'Name Level 3-2'
         }
     ]
]
}

编辑:full XML 的示例 XML,它有一个根,只有一个标题,有 14 组

<Layer queryable="1">
<Title>MSC GeoMet — GeoMet-Weather 2.14.1</Title>
   <Layer queryable="1">
   <Name>Regional Deterministic Prediction System (RDPS) [10 km]</Name>
   <Title>Regional Deterministic Prediction System (RDPS) [10 km]</Title>
      <Layer queryable="1">
      <Name>RDPS - Coupled to Gulf of St. LaWrence (RDPS-CGSL)</Name>
      <Title>RDPS - Coupled to Gulf of St. LaWrence (RDPS-CGSL)</Title>
         <Layer queryable="1" opaque="0" cascaded="0">
         <Name>CGSL.ETA_ICEC</Name>
         <Title>CGSL.ETA.ICEC - Ice cover fraction</Title>
...

解决方法

仅处理 Layer 元素(在该特定命名空间 http://www.opengis.net/wms 中)和第一个 TitleName 以及递归子 Layer 的 XSLT 3 示例s 会

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    xpath-default-namespace="http://www.opengis.net/wms"
    xmlns="http://www.w3.org/2005/xpath-functions"
    expand-text="yes"
    version="3.0">

  <xsl:strip-space elements="*"/>
  
  <xsl:mode on-no-match="shallow-skip"/>

  <xsl:output method="json" build-tree="no" indent="yes"/>
  
  <xsl:template match="/WMS_Capabilities/Capability/Layer" priority="5">
    <xsl:map>
      <xsl:apply-templates/>
    </xsl:map>
  </xsl:template>
  
  <xsl:template match="Layer/Title[1] | Layer/Name[1]">
    <xsl:map-entry key="local-name()" select="data()"/>
  </xsl:template>
  
  <xsl:template match="Layer[1]">
    <xsl:map-entry key="'children'">
      <xsl:sequence select="array { ../Layer/mf:apply-templates(.) }"/>
    </xsl:map-entry>
  </xsl:template>
  
  <xsl:template match="Layer[position() > 1]"/>
  
  <xsl:function name="mf:apply-templates" as="item()*">
    <xsl:param name="elements" as="element(*)*"/>
    <xsl:map>
      <xsl:apply-templates select="$elements/*"/>      
    </xsl:map>
  </xsl:function>
  
</xsl:stylesheet>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。