2

我有一个以空格分隔的坐标元组列表,其中包含任意多个元组。每个元组由一个以空格分隔的二维坐标列表组成。例如,“1.1 2.8 1.2 2.9”表示从 POINT(1.1 2.8) 到 POINT(1.2 2.9) 的一条线。我需要这个改为“1.1,2.8 1.2,2.9”。我将如何使用 XSLT 替换数字对之间的空格到逗号?我有“字符串(gml:LinearRing/gml:posList)”。

这被用在一个 Java Web 服务上,该服务使用几何图形输出 GML 3.1.1 特性。该服务支持可选的 KML 输出,通过使用 XSLT 将 GML 文档转换为 KML 文档(至少,被认为“重要”的块)。我被锁定在 XSLT 1.0 中,因此不能选择 XSLT 2.0 中的正则表达式。

我知道 GML 使用纬度/经度,而 KML 使用经度/纬度。这是在 XSLT 之前处理的,但如果用 XSLT 也能完成它会很好。


感谢您的解决方案,Dimitre。我对其进行了一些修改以满足我的需要,因此我将在此处包含它以防万一它对其他人有所帮助。假设二维元组,它通过坐标列表执行递归。

这执行两个功能:轴交换(纬度/经度为经度/纬度,根据 GML 和 KML 规范)和将每个元组内的坐标分隔符从空格 ' ' 更改为逗号 ','。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gml="http://www.opengis.net/gml" exclude-result-prefixes="gml">
   <xsl:output method="xml" encoding="UTF-8" indent="yes" />

   <!-- other portions omitted -->

   <xsl:template match="gml:pos">
      <xsl:call-template name="coordinateSequence">
         <xsl:with-param name="coords" select="normalize-space(string(.))" />
      </xsl:call-template>
   </xsl:template>

   <xsl:template match="gml:posList">
      <xsl:call-template name="coordinateSequence">
         <xsl:with-param name="coords" select="normalize-space(string(.))" />
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="coordinateSequence">
      <xsl:param name="coords" />
      <xsl:if test="string-length($coords) > 0">
         <xsl:variable name="lat" select="substring-before($coords, ' ')" />
         <xsl:variable name="lon">
            <xsl:value-of select="substring-before(substring-after($coords, ' '), ' ')" />
            <xsl:if test="string-length(substring-before(substring-after($coords, ' '), ' ')) = 0">
               <xsl:value-of select="substring-after($coords, ' ')" />
            </xsl:if>
         </xsl:variable>
         <xsl:variable name="remainder" select="substring-after(substring-after($coords, ' '), ' ')" />

         <xsl:value-of select="concat($lon, ',', $lat)" />
         <xsl:if test="string-length($remainder) > 0">
            <xsl:value-of select="' '" />
            <xsl:call-template name="coordinateSequence">
               <xsl:with-param name="coords" select="$remainder" />
            </xsl:call-template>
         </xsl:if>
      </xsl:if>
   </xsl:template>

</xsl:stylesheet>
4

2 回答 2

3

This transformation (also shows the intermediary steps):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
  <xsl:variable name="vNorm" select="normalize-space()"/>
  <xsl:variable name="vP1" select=
  "concat(substring-before(., ' '), ',',
          substring-before(substring-after($vNorm, ' '),' ')
          )"/>

  <xsl:variable name="vPart2" select="substring-after(substring-after($vNorm,' '),' ')"/>

  <xsl:variable name="vP2" select=
  "concat(substring-before($vPart2, ' '), ',',
          substring-after($vPart2, ' ')
          )"/>


  <xsl:value-of select="$vP1"/>
==========
  <xsl:value-of select="$vP2"/>
==========
  <xsl:value-of select="concat($vP1, ' ', $vP2)"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>1.1 2.8 1.2 2.9</t>

produces the wanted, correct result (the last line):

1.1,2.8
==========
  1.2,2.9
==========
  1.1,2.8 1.2,2.9

For convenience, this code can be placed in to a named template to be called for each wanted Line:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
  <xsl:call-template name="convertLine"/>
 </xsl:template>

 <xsl:template name="convertLine">
  <xsl:param name="pStr" select="."/>

  <xsl:variable name="vNorm" select="normalize-space($pStr)"/>
  <xsl:variable name="vP1" select=
  "concat(substring-before($pStr, ' '), ',',
          substring-before(substring-after($vNorm, ' '),' ')
          )"/>

  <xsl:variable name="vPart2" select="substring-after(substring-after($vNorm,' '),' ')"/>

  <xsl:variable name="vP2" select=
  "concat(substring-before($vPart2, ' '), ',',
          substring-after($vPart2, ' ')
          )"/>


  <xsl:value-of select="concat($vP1, ' ', $vP2)"/>
 </xsl:template>
</xsl:stylesheet>
于 2012-09-20T02:24:02.333 回答
0
<xsl:for-each select="tokenize($in, ' ')">
  <xsl:value-of select="concat($t, if (position() mod 2) = 0 then ' ' else ',')"/>
</xsl:for-each>
于 2012-09-20T08:09:04.910 回答