Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am fairly new to Geoserver 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 geoserver with some PHP which generates the SLD dynamically (i.e. from a database query), will it work? How does Geoserver handle the SLD stored as style?

Thanks in advance

share|improve this question
up vote 7 down vote accepted

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.

share|improve this answer
that sounds perfect for my purpose. I should have thought of that... THANKS! – Barnabe Jun 16 '11 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 link in your post is dead – Barnabe Jun 17 '11 at 14:50
Thanks, seems like they removed the example. Check this instead – underdark Jun 17 '11 at 16:32
You don't need to parse the SLD in OpenLayers. Just send it forward to Geoserver. – underdark Jun 17 '11 at 16:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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