2011-04-03 81 views
1

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

在此先感謝。

+0

問得好,+1。請參閱我的回答,以解釋代碼中的兩個問題,它們的修復和進一步的重構,這些代碼顯着簡化了代碼並使其更具可讀性。 – 2011-04-03 14:42:01

+2

您的「XML文件」不是XML文件,因爲它沒有聲明gmp命名空間。我強烈懷疑你已經簡化了文件,以避免出現像命名空間聲明那樣的「無關緊要」的細節,而且這往往表明你沒有意識到命名空間聲明遠非無關緊要 - 它們通常是問題的核心這個。 – 2011-04-03 19:56:50

回答

2

只需更換

<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> 

說明:至少有兩個問題在這裏:

  1. <xsl:text>不能在其內部包含其他xsl元素 - 只有文本

  2. XPath表達式/gml:coordinates未選擇任何內容,因爲源XML文檔中沒有/gml:coordinates top元素。

此外重構:該代碼可以通過使用*來進一步簡化AVT * 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> 
+0

+1完整答案。 – 2011-04-03 23:16:56