开发者

Transforming GML with XSLT

开发者 https://www.devze.com 2023-02-21 22:28 出处:网络
i was trying to figure out what is wrong with this piece of code, but, after 4 hours, I give up! I tried a lot of different solutions here on stackoverflow and from arround the web, bot none of them w

i was trying to figure out what is wrong with this piece of code, but, after 4 hours, I give up! I tried a lot of different solutions here on stackoverflow and from arround the web, bot none of them worked.

Al开发者_C百科l i'm trying to do, is to put "gml:coordinates" value to the "point" attribute. I guess it has something to do with the namespace. Or something else...

XML file:

<?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 file:

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

And the Ajax script (returns correct results if attribute is set to "-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);

Thanks in advance.


Just replace:

<Coordinate>
  <xsl:attribute name="point">
    <xsl:text>
      <xsl:value-of select="/gml:coordinates" />
    </xsl:text>
  </xsl:attribute>
</Coordinate>

with:

<Coordinate>
  <xsl:attribute name="point">
      <xsl:value-of select="gml:coordinates" />
  </xsl:attribute>
</Coordinate>

Explanation: There are at least two problems here:

  1. <xsl:text> cannot contain other xsl elements inside itself -- only text.

  2. The XPath expression /gml:coordinates selects nothing, because there is no /gml:coordinates top element in the source XML document.

Further refactoring: The code can be further simplified by using *AVT*s (attribute-value templates):

Replace:

<Coordinate>
  <xsl:attribute name="point">
      <xsl:value-of select="gml:coordinates" />
  </xsl:attribute>
</Coordinate>

with:

<Coordinate point="{gml:coordinates}"/>

Replace:

<IndexedFaceSet>
  <xsl:attribute name="coordIndex">
    <xsl:text>0 1 2 3 -1</xsl:text>
  </xsl:attribute>

with:

<IndexedFaceSet coordIndex="0 1 2 3 -1">

The complete code after the corrections and refactorings:

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

and the result is:

<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

精彩评论

暂无评论...
验证码 换一张
取 消