首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从KML到XSLT的相互冲突的KML xmlns属性

从KML到XSLT的相互冲突的KML xmlns属性
EN

Stack Overflow用户
提问于 2017-03-07 21:15:43
回答 2查看 264关注 0票数 0

我有一个XSLT,它的工作是将KML重新格式化为GML。

代码语言:javascript
复制
<?xml version="1.0"  encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.opengis.net/gml" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" exclude-result-prefixes="kml">

    <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes" />

   <!-- Removes all nodes with any empty text -->
  <xsl:template match="*[.='']"/>

  <!-- Removes all nodes with any empty attribute -->
  <xsl:template match="*[@*='']"/>

    <xsl:template match="text()"/>

    <xsl:template match="/">

      <MultiSurface>
        <surfaceMembers>
          <xsl:apply-templates />
        </surfaceMembers>
      </MultiSurface>
    </xsl:template>

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

    <xsl:template match="kml:Point">
        <!--<Point>
            <xsl:apply-templates />
        </Point>-->
    </xsl:template>

    <xsl:template match="kml:LineString">
        <!--<LineString>
            <xsl:apply-templates />
        </LineString>-->
    </xsl:template>

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

    <xsl:template match="kml:outerBoundaryIs">
        <exterior>
            <xsl:apply-templates />
        </exterior>
    </xsl:template>

    <xsl:template match="kml:innerBoundaryIs">
        <interior>
            <xsl:apply-templates />
        </interior>
    </xsl:template>

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

    <xsl:template match="kml:coordinates">
        <posList>
        <!--<xsl:value-of select="translate(., ',', ' ')" />-->
        <xsl:call-template name="output-tokens">
          <xsl:with-param name="list" select="." />
        </xsl:call-template>
        </posList>
    </xsl:template>

    <xsl:template name="output-tokens">
        <xsl:param name="list" />
        <xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" />
        <xsl:variable name="first" select="substring-before($newlist, ' ')" />
        <xsl:variable name="remaining" select="substring-after($newlist, ' ')" />
<!-- long, lat, alt-->
        <xsl:variable name="long" select="substring-before($first, ',')" />

        <xsl:choose>
            <xsl:when test="contains(substring-after($first, ','), ',')">
                <xsl:variable name="lat" select="substring-before(substring-after($first, ','), ',')" />
                <xsl:value-of select="concat($lat, ' ', $long, ' ')" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="lat" select="substring-after($first, ',')" />
                <xsl:value-of select="concat($lat, ' ', $long, ' ')" />
            </xsl:otherwise>
        </xsl:choose>

        <xsl:if test="$remaining">
            <xsl:call-template name="output-tokens">
                <xsl:with-param name="list" select="$remaining" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

通常,我们的客户会给我们KML文件,这些文件有一个kml的开始标记:

代码语言:javascript
复制
<kml xmlns="http://www.opengis.net/kml/2.2">

对于这个例子,XSLT在转换那个KML文件方面发挥了很大的作用。但是我们今天有一个.

代码语言:javascript
复制
<kml xmlns="http://earth.google.com/kml/2.2">...</kml>

这不起作用,我认为这是因为KMLs属性没有设置为:http://www.opengis.net/kml/2.2,或者xmlns:kml没有设置为:http://earth.google.com/kml/2.2

我试过以下几种方法,但都没有用

代码语言:javascript
复制
xmlns:kml="http://www.opengis.net/kml/2.2 http://earth.google.com/kml/2.2" 

我觉得答案将是愚蠢的简单,但我还没有偶然发现它,我已经没有东西可以尝试和谷歌。你们有什么建议?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-07 21:55:40

重要的是要理解XML名称空间前缀本身并不有意义。它们只是名称空间名称的一种简写形式,名称空间名称是标识名称空间的URI。是名称空间名称(通过名称空间声明属性绑定到前缀)实际上标识了名称空间和命名空间感知的XML处理器(如XSLT处理器)中的范围名称。因此,尝试将一个前缀绑定到两个其他名称空间名称是没有意义的。

所有这些都与XML模式文档的位置无关。但是,假设您正在讨论的KML2.2是http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd上的模式文档所描述的KML2.2,那么它的命名空间名称是http://www.opengis.net/kml/2.2,正如它的模式所明确指定的那样。实例文档不能使用不同的名称空间名称(尽管它们可以将它们想要的任何名称空间前缀绑定到名称中-它不必是"kml")。

底线:只有两种可能性:

  1. 客户端提供的文档由于使用了错误的命名空间名称而格式错误。在这种情况下,最好的做法是修复客户端文件中的命名空间名称,或者要求客户端这样做。您可以通过编辑它来实现这一点,也可以编写样式表来执行这样的转换。无论是哪种方式,根据您希望它符合的架构来验证结果文档可能是个好主意。
  2. 客户端提供的文档具有与您预期的不同的XML文档类型(松散的含义),并且准备处理。在这种情况下,唯一要做的事情是从客户端请求一个正确类型的新文件。
票数 3
EN

Stack Overflow用户

发布于 2017-03-08 08:58:42

Google的名称空间是KML标准的扩展:参见https://developers.google.com/kml/documentation/kmlreference

因此,如果您只需更改样式表以处理不同的命名空间,它可能会起作用,也可能不起作用,具体取决于:(a)是否实际使用了Google扩展,以及(b)如何编写样式表来处理扩展(丢弃任何具有空文本或空属性的元素的模板规则看起来并不是一个特别好的开始)。

通常,当您有两个使用不同名称空间的XML词汇表的变体时,我的建议是首先将一个变体预处理为另一个变体。在这种情况下,这是否是正确的策略取决于更详细地了解具体情况。

您不应该做的是尝试编写一个处理这两个变体的样式表。最后,所有地方都有条件逻辑,这会使代码混乱,使调试成为一场噩梦。

但我看到了一种成功使用的替代方法:代替预处理源文档来处理变体词汇表,而是对样式表进行预处理。

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

https://stackoverflow.com/questions/42658497

复制
相关文章

相似问题

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