Exporting a KML file from PHP
<p>I'm facing a weird problem while trying to dynamically create an KML file (which is basically just XML) with PHP. I'm using the laravel framework and here's the view that generates the KML file.</p>

<pre><code>&lt;?php
    header('Content-type: text/plain');
    header('Content-Disposition: attachment; filename="location.kml"');
?&gt;
&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;kml xmlns='http://www.opengis.net/kml/2.2'&gt;
  &lt;Document&gt;
    &lt;name&gt;Towers&lt;/name&gt;

    &lt;?php for ($i = 0; $i &lt; count($points); $i++){ ?&gt;
    &lt;Placemark&gt;
      &lt;name&gt;{{ $points[$i]['name'] }}&lt;/name&gt;
      &lt;!--HERE'S THE PROBLEM--&gt;
      &lt;styleUrl&gt;#icon-503-DB4436-nodesc&lt;/styleUrl&gt;
      &lt;Point&gt;
        &lt;coordinates&gt;{{ $points[$i]['coords'] }}&lt;/coordinates&gt;
      &lt;/Point&gt;
    &lt;/Placemark&gt;
    &lt;?php } ?&gt;

    &lt;?php for ($i = 0; $i &lt; count($paths); $i++){ ?&gt;
    &lt;Placemark&gt;
      &lt;name&gt;{{ $paths[$i]['name'] }}&lt;/name&gt;
      &lt;styleUrl&gt;#line-000000-1-nodesc&lt;/styleUrl&gt;
      &lt;LineString&gt;
        &lt;tessellate&gt;1&lt;/tessellate&gt;
        &lt;coordinates&gt;{{ $paths[$i]['coords'] }}&lt;/coordinates&gt;
      &lt;/LineString&gt;
    &lt;/Placemark&gt;
    &lt;?php } ?&gt;

    &lt;Style id='icon-503-DB4436-nodesc-normal'&gt;
      &lt;IconStyle&gt;
        &lt;color&gt;ff3644DB&lt;/color&gt;
        &lt;scale&gt;1.1&lt;/scale&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x='16' y='31' xunits='pixels' yunits='insetPixels'&gt;
        &lt;/hotSpot&gt;
      &lt;/IconStyle&gt;
      &lt;LabelStyle&gt;
        &lt;scale&gt;0.0&lt;/scale&gt;
      &lt;/LabelStyle&gt;
      &lt;BalloonStyle&gt;
        &lt;text&gt;&lt;![CDATA[&lt;h3&gt;$[name]&lt;/h3&gt;]]&gt;&lt;/text&gt;
      &lt;/BalloonStyle&gt;
    &lt;/Style&gt;
    &lt;Style id='icon-503-DB4436-nodesc-highlight'&gt;
      &lt;IconStyle&gt;
        &lt;color&gt;ff3644DB&lt;/color&gt;
        &lt;scale&gt;1.1&lt;/scale&gt;
        &lt;Icon&gt;
          &lt;href&gt;http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png&lt;/href&gt;
        &lt;/Icon&gt;
        &lt;hotSpot x='16' y='31' xunits='pixels' yunits='insetPixels'&gt;
        &lt;/hotSpot&gt;
      &lt;/IconStyle&gt;
      &lt;LabelStyle&gt;
        &lt;scale&gt;1.1&lt;/scale&gt;
      &lt;/LabelStyle&gt;
      &lt;BalloonStyle&gt;
        &lt;text&gt;&lt;![CDATA[&lt;h3&gt;$[name]&lt;/h3&gt;]]&gt;&lt;/text&gt;
      &lt;/BalloonStyle&gt;
    &lt;/Style&gt;
    &lt;StyleMap id='icon-503-DB4436-nodesc'&gt;
      &lt;Pair&gt;
        &lt;key&gt;normal&lt;/key&gt;
        &lt;styleUrl&gt;#icon-503-DB4436-nodesc-normal&lt;/styleUrl&gt;
      &lt;/Pair&gt;
      &lt;Pair&gt;
        &lt;key&gt;highlight&lt;/key&gt;
        &lt;styleUrl&gt;#icon-503-DB4436-nodesc-highlight&lt;/styleUrl&gt;
      &lt;/Pair&gt;
    &lt;/StyleMap&gt;
    &lt;Style id='line-000000-1-nodesc-normal'&gt;
      &lt;LineStyle&gt;
        &lt;color&gt;ff000000&lt;/color&gt;
        &lt;width&gt;1&lt;/width&gt;
      &lt;/LineStyle&gt;
      &lt;BalloonStyle&gt;
        &lt;text&gt;&lt;![CDATA[&lt;h3&gt;$[name]&lt;/h3&gt;]]&gt;&lt;/text&gt;
      &lt;/BalloonStyle&gt;
    &lt;/Style&gt;
    &lt;Style id='line-000000-1-nodesc-highlight'&gt;
      &lt;LineStyle&gt;
        &lt;color&gt;ff000000&lt;/color&gt;
        &lt;width&gt;2.0&lt;/width&gt;
      &lt;/LineStyle&gt;
      &lt;BalloonStyle&gt;
        &lt;text&gt;&lt;![CDATA[&lt;h3&gt;$[name]&lt;/h3&gt;]]&gt;&lt;/text&gt;
      &lt;/BalloonStyle&gt;
    &lt;/Style&gt;
    &lt;StyleMap id='line-000000-1-nodesc'&gt;
      &lt;Pair&gt;
        &lt;key&gt;normal&lt;/key&gt;
        &lt;styleUrl&gt;#line-000000-1-nodesc-normal&lt;/styleUrl&gt;
      &lt;/Pair&gt;
      &lt;Pair&gt;
        &lt;key&gt;highlight&lt;/key&gt;
        &lt;styleUrl&gt;#line-000000-1-nodesc-highlight&lt;/styleUrl&gt;
      &lt;/Pair&gt;
    &lt;/StyleMap&gt;
  &lt;/Document&gt;
&lt;/kml&gt;
</code></pre>

<p>I exported a single layer from google maps and changed it as how I wanted. And this works just fine. But the only problem is that the first occurrence <code>&lt;styleUrl&gt;</code> tag in the code, when the file is downloaded, is just <code>Url&gt;</code>.</p>

<p>There are also few other <code>&lt;styleUrl&gt;</code> tags but they work just fine. I tried remove all the PHP stuff and putting just static KML but still it outputs <code>Url&gt;</code>. Any idea what could be wrong?</p>

<p><strong>Edit 1 :</strong></p>

<p>Variables being passed to the template are <code>$paths</code> and <code>$points</code>. Here they are in case it's any help. </p>

<pre><code>// $points
array(1) {
  [0]=&gt;
  array(2) {
    ["name"]=&gt;
    string(9) "Nijraj Gelani"
    ["coords"]=&gt;
    string(29) "-79.58523260,43.73280100,0.0 "
  }
}
// $paths
array(0) {
}
</code></pre>

<p><strong>Edit 2 :</strong></p>

<p>I tried to move the <code>&lt;styleUrl&gt;</code> tag up a little bit (although that is syntactically incorrect but just to get the idea) and it works fine as long as it's not child of <code>&lt;kml&gt;</code> tag in any way. i.e. putting it after <code>&lt;?xml?&gt;</code> tag works fine.</p>