1

我试图弄清楚这段代码有什么问题,但是,4小时后,我放弃了!我在 stackoverflow 上尝试了很多不同的解决方案,并且从 arround 网络上尝试了很多不同的解决方案,但它们都不起作用。

我要做的就是将“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 脚本(如果属性设置为“/gml:coordinates”的“-7 -7 0 7 -7 0 7 7 0 -7 7 0”,则返回正确的结果):

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

提前致谢。

4

1 回答 1

3

只需更换

<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顶部元素。

进一步重构:代码可以通过使用* 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>
于 2011-04-03T14:40:36.880 回答