2011-04-03 83 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