1

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.

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

CubicsRube
  • 71
  • 1
  • 3
  • 10
  • Good question, +1. See my answer for explanation of the two problems in your code, their fixes and further refactoring that simplifies the code significantly and makes it more readable. – Dimitre Novatchev Apr 03 '11 at 14:42
  • 2
    Your "XML file" isn't an XML file, because it doesn't declare the gmp namespace. I strongly suspect you've simplified the file to leave out "irrelvant" detail like the namespace declaration, and that tends to suggest you've failed to realise that namespace declarations are very far from irrelevant - they are often at the heart of problems like this. – Michael Kay Apr 03 '11 at 19:56

1 Answers1

3

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>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431