首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT转换GML

使用XSLT转换GML
EN

Stack Overflow用户
提问于 2011-04-03 19:14:38
回答 1查看 1.5K关注 0票数 1

我试图找出这段代码的错误所在,但是,4个小时后,我放弃了!我在stackoverflow上尝试了很多不同的解决方案,从网络周围看,它们都不起作用。

我所要做的就是将"gml:coordinates“值赋给"point”属性。我猜这与命名空间有关。或者别的什么..。

XML文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="ISO-8859-1"?>
<gml:LineString>
    <gml:coordinates>-7 -7 0 7 -7 0 7 7 0 -7 7 0</gml:coordinates>
</gml:LineString>

XSL文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:gml="http://www.opengis.net/gml">

<xsl:template match="/gml:LineString">
    <Transform>
        <Shape>
            <IndexedFaceSet>
                <xsl:attribute name="coordIndex">
                    <xsl:text>0 1 2 3 -1</xsl:text>
                </xsl:attribute>

                <Coordinate>
                    <xsl:attribute name="point">
                        <xsl:text>
                            <xsl:value-of select="/gml:coordinates" />
                        </xsl:text>
                    </xsl:attribute>
                </Coordinate>
            </IndexedFaceSet>
        </Shape>
    </Transform>
</xsl:template>
</xsl:stylesheet> 

和Ajax脚本(如果属性设置为"-7 -7 0 7 -7 0 7 7 0 -7 7 0-7 7 0“insted of”/gml:coordinates“,则返回正确的结果):

代码语言:javascript
复制
var xml = document.implementation.createDocument("", "", null);
var xsl = document.implementation.createDocument("", "", null);
xml.async = false;
xsl.async = false;
xml.load("xsl/ajax.xml");
xsl.load("xsl/ajax.xsl");
var processor = new XSLTProcessor();
processor.importStylesheet(xsl);
var output = processor.transformToFragment(xml, document);
document.getElementById("scene").appendChild(output);

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-03 22:40:37

只需替换

代码语言:javascript
复制
<Coordinate>
  <xsl:attribute name="point">
    <xsl:text>
      <xsl:value-of select="/gml:coordinates" />
    </xsl:text>
  </xsl:attribute>
</Coordinate>

使用的

代码语言:javascript
复制
<Coordinate>
  <xsl:attribute name="point">
      <xsl:value-of select="gml:coordinates" />
  </xsl:attribute>
</Coordinate>

解释:这里至少有两个问题:

  1. 表达式本身不能包含其他xsl元素--只有text.
  2. The XPath expression /gml:coordinates不选择任何内容,因为源/gml:coordinates文档中没有顶层元素。

进一步重构:使用**s (属性-值模板)可以进一步简化代码:

替换

代码语言:javascript
复制
<Coordinate>
  <xsl:attribute name="point">
      <xsl:value-of select="gml:coordinates" />
  </xsl:attribute>
</Coordinate>

使用的

代码语言:javascript
复制
<Coordinate point="{gml:coordinates}"/>

替换

代码语言:javascript
复制
<IndexedFaceSet>
  <xsl:attribute name="coordIndex">
    <xsl:text>0 1 2 3 -1</xsl:text>
  </xsl:attribute>

使用

代码语言:javascript
复制
<IndexedFaceSet coordIndex="0 1 2 3 -1">

在校正和重构之后完整的代码

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:gml="http://www.opengis.net/gml">
    <xsl:template match="/gml:LineString">
        <Transform>
            <Shape>
                <IndexedFaceSet coordIndex="0 1 2 3 -1">
                    <Coordinate point="{gml:coordinates}"/>
                </IndexedFaceSet>
            </Shape>
        </Transform>
    </xsl:template>
</xsl:stylesheet>

,结果是

代码语言:javascript
复制
<Transform xmlns:gml="http://www.opengis.net/gml">
    <Shape>
        <IndexedFaceSet coordIndex="0 1 2 3 -1">
            <Coordinate point="-7 -7 0 7 -7 0 7 7 0 -7 7 0"/>
        </IndexedFaceSet>
    </Shape>
</Transform>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5529239

复制
相关文章

相似问题

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