0

我正在尝试将 GeoMet WMS XML response.data 的 GetCapabilities 转换为可在v-treeviewVuetify 组件中使用的 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>innerHTML 的东西,<Layer>以及它们在数组中的子元素,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'
         }
     ]
]
}

编辑:完整 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>
...
4

1 回答 1

1

Layer仅处理元素(在该特定命名空间中)的 XSLT 3 示例,http://www.opengis.net/wms第一个TitleName递归地加上子Layers 将是

<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>
于 2021-05-17T19:09:03.567 回答