使用 XSLT 将 KML 转换为另一种格式

converting KML to another format using XSLT

我正在尝试将从 JAK(使用默认的 ns3 命名空间)生成的 KML 转换为没有命名空间元素的 KML。参考了 XSLT 相关问题,找到了一个 XSLT :

   <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns3="http://www.opengis.net/kml/2.2">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="ns3:kml">
     <xsl:copy >
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name(.)}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="." />
    </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

我的原始 KML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:kml xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:ns3="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <ns3:Document>
        <ns3:name>testTrail</ns3:name>
       </ns3:Document>
</ns3:kml>

转换结果:

    <?xml version="1.0" encoding="UTF-8"?>
<ns3:kml xmlns:ns3="http://www.opengis.net/kml/2.2" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
        <Document>
            <name>testTrail</name>
    </Document>
    </kml>

期望的结果:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
        <Document>
            <name>testTrail</name>
    </Document>
    </kml>

如何确保在 kml 标记中包含命名空间 xmlns? 尝试在 XSLT 中使用不同的组合。请帮忙。已经尝试了很长时间了。

尝试了@zx485 建议的 xslt 结果 xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?><ns0:kml xmlns:ns0="http://www.opengis.net/kml/2.2">
    <ns1:Document xmlns:ns1="http://www.opengis.net/kml/2.2">
        <ns2:name xmlns:ns2="http://www.opengis.net/kml/2.2">testTrail</ns2:name>

我不希望 ns1 ns2 .. 命名空间作为标签前缀。

像这样将 namespace-uri() 添加到 XSLT 的 xsl:element 中:

<xsl:template match="*">
  <xsl:element name="{local-name(.)}" namespace="{namespace-uri()}">
    <xsl:value-of select="." />
    <xsl:apply-templates select="@* | node()" />
  </xsl:element>
</xsl:template>
<xsl:template match="@*">
  <xsl:attribute name="{local-name(.)}">
    <xsl:value-of select="." />
  </xsl:attribute>
</xsl:template>
<xsl:template match="text()">
    <xsl:value-of select="." />
</xsl:template>

这使用当前匹配节点的名称空间并复制 text() 个节点。

编辑: 我刚刚使用了上面的代码片段。以下模板已删除,一切似乎都按要求工作。

<xsl:template match="ns3:kml">
  <xsl:copy >
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

输出为

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
        <name>testTrail</name>
       </Document>
</kml>