我想弄清楚这段代码有什么问题,但是4小时后我放弃了!我在这里尝试了很多不同的解决方案,从web,bot都无法工作。用XSLT转换GML
我想要做的是将“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>
而阿贾克斯脚本(返回正确的结果,如果属性被设置为“-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);
在此先感谢。
问得好,+1。请参阅我的回答,以解释代码中的两个问题,它们的修复和进一步的重构,这些代码显着简化了代码并使其更具可读性。 – 2011-04-03 14:42:01
您的“XML文件”不是XML文件,因为它没有声明gmp命名空间。我强烈怀疑你已经简化了文件,以避免出现像命名空间声明那样的“无关紧要”的细节,而且这往往表明你没有意识到命名空间声明远非无关紧要 - 它们通常是问题的核心这个。 – 2011-04-03 19:56:50