I would like to use inline SLD to style polygon served by GeoServer.
Here is working example in MapServer:
without defined style:
http://gis.ibbeck.de/include/mapserver/mapserv.exe?map=/daten/mapfiles/world/World.map&LAYERS=COUNTRY&FORMAT=image%2Fpng&VERSION=1.1.1&TRANSPARENT=TRUE&SERVICE=WMS&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-52,-90,76,38&WIDTH=256&HEIGHT=256
with SLD_BODY parameter:
http://gis.ibbeck.de/include/mapserver/mapserv.exe?map=/daten/mapfiles/world/World.map&LAYERS=COUNTRY&FORMAT=image%2Fpng&VERSION=1.1.1&TRANSPARENT=TRUE&SLD_BODY=%3CStyledLayerDescriptor+version%3D%221.1.0%22%3E%3CNamedLayer%3E%3CName%3ECountry%3C%2FName%3E%3CUserStyle%3E%3CIsDefault%3E1%3C%2FIsDefault%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CPolygonSymbolizer%3E%3CFill%3E%3CCssParameter+name%3D%22fill%22%3E%23FF0000%3C%2FCssParameter%3E%3C%2FFill%3E%3C%2FPolygonSymbolizer%3E%3C%2FRule%3E%3C%2FFeatureTypeStyle%3E%3C%2FUserStyle%3E%3C%2FNamedLayer%3E%3C%2FStyledLayerDescriptor%3E%0A&SERVICE=WMS&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-52,-90,76,38&WIDTH=256&HEIGHT=256
Here is unencoded SLD_BODY parameter:
<StyledLayerDescriptor version="1.1.0">
<NamedLayer>
<Name>Country</Name>
<UserStyle>
<IsDefault>1</IsDefault>
<FeatureTypeStyle>
<Rule>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#FF0000</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
According to documentation, the same SLD should work for GeoServer serving WMS:
Library mode occurs when map layers and styles are specified using the layers and styles WMS parameters, and additional styling is supplied externally...The styles in the external style document take precedence over the catalog styles during rendering. Style lookup in library mode operates as follows:
For each layer in the layers list, the applied style is either a named style specified in the styles list (if present), or the layer default style
For a named style, if the external style document has a
<NamedLayer>...<UserStyle>
with matching layer name and style name, then it is used. Otherwise, the style name is searched for in the catalog. If it is not found there, an error occurs.For a default style, the external style document is searched to find a
<NamedLayer>
element with the layer name. If it contains a<UserStyle>
with the<IsDefault>
element having the value 1 then that style is used. Otherwise, the default server style for the layer (which must exist) is used.
But I can't get it working (i.e. all polygons should be red but remain unchanged):
http://ihp-wins.unesco.org/geoserver/geonode/wms?service=WMS&version=1.1.1&request=GetMap&layers=DRIN_GWBs&bbox=6586968.00981259%2C4614357.59391692%2C6647685.57959369%2C4705644.12428735&width=510&height=768&srs=EPSG%3A3908&format=image%2Fpng&SLD_BODY=%3CStyledLayerDescriptor+version%3D%221.1.0%22%3E%3CNamedLayer%3E%3CName%3EDRIN_GWBs%3C%2FName%3E%3CUserStyle%3E%3CIsDefault%3E1%3C%2FIsDefault%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CPolygonSymbolizer%3E%3CFill%3E%3CCssParameter+name%3D%22fill%22%3E%23FF0000%3C%2FCssParameter%3E%3C%2FFill%3E%3C%2FPolygonSymbolizer%3E%3C%2FRule%3E%3C%2FFeatureTypeStyle%3E%3C%2FUserStyle%3E%3C%2FNamedLayer%3E%3C%2FStyledLayerDescriptor%3E
Here is that unencoded SLD parameter:
<StyledLayerDescriptor version="1.1.0">
<NamedLayer>
<Name>DRIN_GWBs</Name>
<UserStyle>
<IsDefault>1</IsDefault>
<FeatureTypeStyle>
<Rule>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#FF0000</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
SLD is supported by this service (as per GetCapabilities):
<UserDefinedSymbolization SupportSLD="1" UserLayer="1" UserStyle="1" RemoteWFS="1"/>
And GetStyles as well:
http://ihp-wins.unesco.org/geoserver/geonode/wms?service=WMS&version=1.1.1&request=GetStyles&layers=DRIN_GWBs
There are no errors but polygons are not changing color either.
= = = = Edit 2021.02.18
@Ian-Turton solution does work. But there are a few quirks that I will try to explain to those lost as I am:
Here is working url for another service with inline SLD_BODY parameter:
https://hotspots.dea.ga.gov.au/geoserver/public/wms?srs=EPSG%3A3857&format=image%2Fpng&service=WMS&version=1.1.1&request=GetMap&layers=DigitalEarthAustraliaWaterbodies&bbox=15262945.80798399%2C-3443946.7464169003%2C15341217.324948017%2C-3365675.229452882&width=256&height=256&SLD_BODY=%3CStyledLayerDescriptor%20version%3D%221.0.0%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Agml%3D%22http%3A%2F%2Fwww.opengis.net%2Fgml%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20http%3A%2F%2Fschemas.opengis.net%2Fsld%2F1.0.0%2FStyledLayerDescriptor.xsd%22%3E%3CNamedLayer%3E%3CName%3Epublic:DigitalEarthAustraliaWaterbodies%3C%2FName%3E%3CUserStyle%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CPolygonSymbolizer%3E%3CFill%3E%3CCssParameter%20name%3D%22fill%22%3E%23ff0000%3C%2FCssParameter%3E%3C%2FFill%3E%3C%2FPolygonSymbolizer%3E%3C%2FRule%3E%3C%2FFeatureTypeStyle%3E%3C%2FUserStyle%3E%3C%2FNamedLayer%3E%3C%2FStyledLayerDescriptor%3E
And SLD part, unencoded:
<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
<NamedLayer>
<Name>public:DigitalEarthAustraliaWaterbodies</Name>
<UserStyle>
<FeatureTypeStyle>
<Rule>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#FF0000</CssParameter>
</Fill>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
So <Name>
is indeed layer name but must have 'workspace prefix' included, as Ian indicated in his comment (ie geoserver/public/wms). It's not something that is listed in GetCapabilities and can only be 'deducted' from service url. That requirement was not obvious to me.
I would also like to point that this solution may be 'undocumented feature' of Geoserver since, in my opinion, it does not match official documentation.
That is, it is neither the second dot option (ie. where SLD supposed to have "matching layer name AND style name" - since only layer name is provided) nor the third dot point (ie. "contains a <UserStyle>
with the <IsDefault>
element having the value 1" - no <IsDefault>
element is present in SLD).
styles
parameter which matches the name of the style in the SLD