I am fairly new to GeoSever and I have a question before I launch into something bigger than me: if I replace the XML code in the SLD style sheet inside GeoSever with some PHP which generates the SLD dynamically (i.e. from a database query), will it work?

How does GeoSever handle the SLD stored as style?

1 Answer 1


No, that won't work. Geoserver is expecting an SLD, so it will fail parsing any PHP code you insert there.

What you can do is generate the SLD outside of Geoserver and pass it over when requesting the map from Geoserver. In OpenLayers it works like this:

var sld = '<StyledLayerDescriptor version ="1.0.0" ...> more xml here </StyledLayerDescriptor>';  

wms = new OpenLayers.Layer.WMS.Post(
      'layers': 'myNs:layername',
      format: 'image/jpeg',
      sld_body: sld
      unsupportedBrowsers: []

More info: Dynamic Styling and Filtering of a Geoserver WMS Using OpenLayers.Layer.WMS.Post

Update: You can find a lot of SLD examples in Geoserver's SLD Cookbook.

  • that sounds perfect for my purpose. I should have thought of that... THANKS!
    – Barnabe
    Jun 16, 2011 at 8:48
  • Underdark, i am having trouble replicating your result if i simply define a variable called sld and link it to an external file (xml) and then specify an sld or sld_body option for my layer OL/geoserver ignore it completely. I have started reading about sld and it sounds complex. It seems that i need to use something like OpenLayers.Format.SLD().read. Would you mind linking to the working example you mention in your post? Thanks. PS: the openlayers.org/dev/examples/WMSPost.html link in your post is dead
    – Barnabe
    Jun 17, 2011 at 14:50
  • Thanks, seems like they removed the example. Check this instead openlayers.org/dev/examples/wms-long-url.html
    – underdark
    Jun 17, 2011 at 16:32
  • You don't need to parse the SLD in OpenLayers. Just send it forward to Geoserver.
    – underdark
    Jun 17, 2011 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.