1

It's been a few years since I've done any coding so I hope you will bear with me...

I have an app that outputs the (non-standard) kml file below.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
    <Style id="icon-503-BCA920">
        <IconStyle>
            <color>FFBCA920</color>
            <Icon><href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href></Icon>
    </IconStyle>
    </Style>
    <Placemark>
        <name><![CDATA[Deficiency 2]]></name>
        <styleUrl>#icon-503-BCA920</styleUrl>

        <ExtendedData>
            <Data name="rating"><value>0</value></Data>
            <Data name="images"><value>file:///storage/emulated/0/mapin/1411660694536.jpg||</value></Data>
        </ExtendedData>

        <description><![CDATA[<p dir="ltr">4001; Vegetation Control; Mowing; + 60 &lt; 2m</p>
          <br/><img src="images/image_1.jpg"/>
          <br/><img src="images/image_1.jpg"/>
        ]]></description>

        <Point>
            <coordinates>-89.59504411, 48.0247752, 0</coordinates>
        </Point>
    </Placemark>

I am trying to use xsl to convert/output it as an html page/table. What I have gleaned so far is my code should like something like this:

<html>
<body>

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">

<xsl:template match="/">

 <h2>Audit Results</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:center">Name</th>
        <th style="text-align:center">Description</th>
        <th style="text-align:center">Coordinates</th>
      </tr>
      <xsl:for-each select="kml:kml/dml:Document/kml:Placemark">
      <tr>
        <td><xsl:value-of disable-output-escaping="yes" select="kml:name"/></td>
        <td><xsl:value-of disable-output-escaping="yes" select="kml:description"/></td>
        <td><xsl:value-of select="kml:Point/kml:coordinates"/></td>
      </tr>
      </xsl:for-each>
    </table>
</xsl:template>
</xsl:stylesheet>

</body>
</html>

Having read everything i can lay my hands on about xsl the last day or two I am at my ropes end. What I'm hoping someone can tell me is: 1)do I have to call the kml file i would like to display?? it is a local file. 2)am I going about this wrong? is there any articles you know of which could help me?

What I am attempting to do is use the xsl code to output a html file that I can print every time I finish another "audit.kml" file. Is there a better/easier way you would recommend doing this?

I am honestly looking to learn how to do this myself, not here asking for a block of code... all advice is greatly appreciated!


Thanks Phil. So now that I have a valid xsl file I am trying to apply it to my kml file locally using the browser. Some reading points me in the direction of Javascript being the best way to do this. When I use the script below I come up with a blank document... any advice?

<html>
<head>
<script type="text/javascript">
function loadXMLDoc(filename)
{
if (window.ActiveXObject)
  {
  xhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
else 
  {
  xhttp = new XMLHttpRequest();
  }
xhttp.open("GET", filename, false);
try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}

function displayResult()
{
xml = loadXMLDoc("doc.kml");
xsl = loadXMLDoc("reportGen.xsl");
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
  {
  ex = xml.transformNode(xsl);
  document.getElementById("example").innerHTML = ex;
  }
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xsltProcessor = new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);
  resultDocument = xsltProcessor.transformToFragment(xml, document);
  document.getElementById("example").appendChild(resultDocument);
  }
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>
JGilpin
  • 13
  • 3

1 Answers1

0

You are close. You should have the xsl:stylesheet as your root. Also you have a typo in your name space

<xsl:for-each select="kml:kml/dml:Document/kml:Placemark">

should be

<xsl:for-each select="kml:kml/kml:Document/kml:Placemark">

You want something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <xsl:template match="/">
        <html>
            <head>
                <title/>
            </head>
            <body>
                <h2>Audit Results</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th style="text-align:center">Name</th>
                        <th style="text-align:center">Description</th>
                        <th style="text-align:center">Coordinates</th>
                    </tr>
                    <xsl:for-each select="kml:kml/kml:Document/kml:Placemark">
                        <tr>
                            <td>
                                <xsl:value-of disable-output-escaping="yes" select="kml:name"/>
                            </td>
                            <td>
                                <xsl:value-of disable-output-escaping="yes" select="kml:description"/>
                            </td>
                            <td>
                                <xsl:value-of select="kml:Point/kml:coordinates"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
  • Thanks Phil. So now that I have a valid xsl file I am trying to apply it to my kml file locally using the browser. Some reading points me in the direction of Javascript being the best way to do this. When I use the script below I come up with a blank document... any advice? – JGilpin Oct 21 '14 at 15:41
  • @JGilpin - see http://stackoverflow.com/questions/3466854/which-browser-can-show-xml-data-transformed-by-xslt – PhillyNJ Oct 21 '14 at 15:46