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

How to add SLD style which is written in String format and stored in a 'var' variable of javascript and asign it to a WMS layer while creating it.

share|improve this question
up vote 3 down vote accepted

All you need to do is replace the styles parameter with an SLD parameter that contains your style. The following code from this example demonstrates the concept.

            theSLD_BODY= '<?xml version="1.0" encoding="utf-8"?>' + '<StyledLayerDescriptor version="1.0.0" xmlns="" xmlns:gml="" xmlns:ogc="" xmlns:xlink="" xmlns:xsi="" xsi:schemaLocation=""><NamedLayer><Name>COUNTRY</Name><UserStyle><FeatureTypeStyle><Rule><ogc:Filter><ogc:PropertyIsLike wildCard="*" singleChar="#" escape="!"><ogc:PropertyName>Continent</ogc:PropertyName><ogc:Literal>Africa</ogc:Literal></ogc:PropertyIsLike></ogc:Filter><PolygonSymbolizer><Fill><CssParameter name="fill">#5599DD</CssParameter></Fill><Stroke><CssParameter name="stroke">#000000</CssParameter></Stroke></PolygonSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>';
        layer2 = new OpenLayers.Layer.WMS( "Africa (SLD_BODY)",
                {   "layers": "COUNTRY",
                    "format": "image/png",
                    "version": "1.1.1",
                    "transparent": "TRUE",
                    "SLD_BODY": theSLD_BODY

You may encounter problems if the SLD becomes too long as it may exceed the maximum URL length of your browser. In which case you will need to switch to POST instead of GET requests.

share|improve this answer
thanks iant, it worked. – DileepKumar Jan 8 '13 at 20:33

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.