我试图找出这段代码的错误所在,但是,4个小时后,我放弃了!我在stackoverflow上尝试了很多不同的解决方案,从网络周围看,它们都不起作用。
我所要做的就是将"gml:coordinates“值赋给"point”属性。我猜这与命名空间有关。或者别的什么..。
XML文件:
<?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文件:
<?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“,则返回正确的结果):
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);提前谢谢。
发布于 2011-04-03 22:40:37
只需替换
<Coordinate>
<xsl:attribute name="point">
<xsl:text>
<xsl:value-of select="/gml:coordinates" />
</xsl:text>
</xsl:attribute>
</Coordinate>使用的
<Coordinate>
<xsl:attribute name="point">
<xsl:value-of select="gml:coordinates" />
</xsl:attribute>
</Coordinate>解释:这里至少有两个问题:
/gml:coordinates不选择任何内容,因为源/gml:coordinates文档中没有顶层元素。进一步重构:使用**s (属性-值模板)可以进一步简化代码:
替换
<Coordinate>
<xsl:attribute name="point">
<xsl:value-of select="gml:coordinates" />
</xsl:attribute>
</Coordinate>使用的
<Coordinate point="{gml:coordinates}"/>替换
<IndexedFaceSet>
<xsl:attribute name="coordIndex">
<xsl:text>0 1 2 3 -1</xsl:text>
</xsl:attribute>使用的
<IndexedFaceSet coordIndex="0 1 2 3 -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 coordIndex="0 1 2 3 -1">
<Coordinate point="{gml:coordinates}"/>
</IndexedFaceSet>
</Shape>
</Transform>
</xsl:template>
</xsl:stylesheet>,结果是
<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>https://stackoverflow.com/questions/5529239
复制相似问题