我有一个XSLT,它的工作是将KML重新格式化为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的开始标记:
<kml xmlns="http://www.opengis.net/kml/2.2">对于这个例子,XSLT在转换那个KML文件方面发挥了很大的作用。但是我们今天有一个.
<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。
我试过以下几种方法,但都没有用
xmlns:kml="http://www.opengis.net/kml/2.2 http://earth.google.com/kml/2.2" 我觉得答案将是愚蠢的简单,但我还没有偶然发现它,我已经没有东西可以尝试和谷歌。你们有什么建议?
发布于 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")。
底线:只有两种可能性:
发布于 2017-03-08 08:58:42
Google的名称空间是KML标准的扩展:参见https://developers.google.com/kml/documentation/kmlreference
因此,如果您只需更改样式表以处理不同的命名空间,它可能会起作用,也可能不起作用,具体取决于:(a)是否实际使用了Google扩展,以及(b)如何编写样式表来处理扩展(丢弃任何具有空文本或空属性的元素的模板规则看起来并不是一个特别好的开始)。
通常,当您有两个使用不同名称空间的XML词汇表的变体时,我的建议是首先将一个变体预处理为另一个变体。在这种情况下,这是否是正确的策略取决于更详细地了解具体情况。
您不应该做的是尝试编写一个处理这两个变体的样式表。最后,所有地方都有条件逻辑,这会使代码混乱,使调试成为一场噩梦。
但我看到了一种成功使用的替代方法:代替预处理源文档来处理变体词汇表,而是对样式表进行预处理。
https://stackoverflow.com/questions/42658497
复制相似问题