我想弄清楚這段代碼有什麼問題,但是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