从KML到XSLT的KML xmlns属性冲突 [英] Conflicting KML xmlns attribute from KML to XSLT

查看:130
本文介绍了从KML到XSLT的KML xmlns属性冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an XSLT which has the job of reformatting KML to GML.

<?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文件,例如:

Normally our clients have given us KML files that have a kml opening tag as such:

<kml xmlns="http://www.opengis.net/kml/2.2">

在这种情况下,XSLT可以很好地转换KML文件.但是今天我们有一个...

And for this instance the XSLT works great to transform that KML file. However we got one today that had...

<kml xmlns="http://earth.google.com/kml/2.2">...</kml>

这不起作用,我认为这是因为KML的xmlns属性未设置为: http: //www.opengis.net/kml/2.2 或XSLTs xmlns:kml未设置为:

This does not work and I assume it is because the KMLs xmlns attribute is not set to: http://www.opengis.net/kml/2.2 or alternatively the XSLTs xmlns:kml is not set to: http://earth.google.com/kml/2.2

我尝试了以下操作,但没有成功

I tried the following but it did not work

xmlns:kml="http://www.opengis.net/kml/2.2 http://earth.google.com/kml/2.2" 

我觉得答案很简单,但是我还没有偶然发现,我已经没有足够的东西来尝试使用Google了.你们有什么建议?

I feel the answer will be stupidly simple but I have not stumbled across it yet and I am running out of stuff to try and Google. What do you guys suggest?

推荐答案

重要的是要了解XML名称空间前缀并不是天生有意义的.它们仅是名称空间名称的简写形式,名称空间名称是标识名称空间的URI.它是通过名称空间声明属性绑定到前缀的名称空间 name ,它实际上识别名称空间并在支持名称空间的XML处理器(例如XSLT处理器)中作用域名称.因此,尝试将一个前缀绑定到两个备用名称空间名称是没有意义的.

It is important to understand that XML namespace prefixes are not inherently meaningful. They are merely a form of shorthand for namespace names, which are URIs that identify namespaces. It is the namespace name, as bound to a prefix via a namespace declaration attribute, that actually identifies the namespace and scopes names in a namespace-aware XML processor, such as an XSLT processor. As such, it does not make sense to try to bind one prefix to two alternative namespace names.

这与XML模式文档的位置没有任何关系.但是,假设您正在谈论的KML 2.2是架构文档在

None of this has anything to do with the location of XML schema documents. Supposing, however, that the KML 2.2 you're talking about is the one described by the schema document at http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd, its namespace name is http://www.opengis.net/kml/2.2, as is expressly specified by its schema. Instance documents are not at liberty to use a different namespace name (though they are at liberty to bind whatever namespace prefix they want to that name -- it doesn't have to be "kml").

底线:只有两种可能性:

Bottom line: there are only two possibilities:

  1. 由于使用错误的名称空间名称,导致客户提供的文档格式错误.在这种情况下,最好的办法是在客户端文件中固定名称空间名称,或要求客户端这样做.您可以通过对其进行编辑来实现,也可以编写样式表来执行这种转换.无论哪种方式,都可以根据您期望其遵循的架构来验证生成的文档,这是一个好主意.

  1. The document provided by your client is malformed as a result of using the wrong namespace name. In this case, the best thing to do is to fix the namespace name in the client's file, or ask the client to do so. You could do that by editing it, or you could write a stylesheet to perform such a transformation. Either way, it might be a good idea to validate the resulting document against the schema to which you expect it to conform.

您的客户端提供的文档具有与您期望的不同的XML文档类型(松散的意义),并准备处理.在这种情况下,唯一要做的就是从客户端请求正确类型的新文件.

The document provided by your client is of a different XML document type (loose sense) than you expect and are prepared to handle. In this case, the only thing to do is to request a new file of the correct type from the client.

这篇关于从KML到XSLT的KML xmlns属性冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆