2

I want to change SLD of WMS layer dynamically. In SLD string I have CssParameter XML tag that has Hex color code like "#ff0000". When I post it to GeoServer I got this error:

java.lang.RuntimeException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 619; XML document structures must start and end within the same entity. org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 619; XML document structures must start and end within the same entity. XML document structures must start and end within the same entity

That refers to line the line that it has the # character. Here is the code sample:

var SLD = '<?xml version="1.0" encoding="UTF-8"?>  <StyledLayerDescriptor version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" 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:se="http://www.opengis.net/se">';
SLD += '<NamedLayer><Name>pmo:iczmoagriculturemokran</Name><UserStyle><Name>pmo:iczmoagriculturemokran</Name><Title>Green polygon</Title><Abstract>Green fill with black outline</Abstract><FeatureTypeStyle><Rule><PolygonSymbolizer><Fill><CssParameter name="fill">#ff0000</CssParameter></Fill><Stroke /></PolygonSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>';

SLD = encodeURI(SLD);
var source = layer.WMSLayer.getSource();
source.updateParams({ STYLES: undefined, SLD_BODY: SLD });

After encodeURI SLD turns to:

%3C?xml%20version=%221.0%22%20encoding=%22UTF-8%22?%3E%3CStyledLayerDescriptor%20version=%221.0.0%22%20xmlns=%22http://www.opengis.net/sld%22%20xmlns:ogc=%22http://www.opengis.net/ogc%22%20xmlns:xlink=%22http://www.w3.org/1999/xlink%22%20xmlns:xsi=%22http://www.w3.org/2001/XMLSchema-instance%22%20xsi:schemaLocation=%22http://www.opengis.net/sld%20http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd%22%3E%3CNamedLayer%3E%3CName%3Egreen%3C/Name%3E%3CUserStyle%3E%3CName%3Egreen%3C/Name%3E%3CTitle%3EGreen%20polygon%3C/Title%3E%3CAbstract%3EGreen%20fill%20with%20black%20outline%3C/Abstract%3E%3CFeatureTypeStyle%3E%3CRule%3E%3CPolygonSymbolizer%3E%3CFill%3E%3CCssParameter%20name=%22fill%22%3E#ff0000%3C/CssParameter%3E%3C/Fill%3E%3CStroke%20/%3E%3C/PolygonSymbolizer%3E%3C/Rule%3E%3C/FeatureTypeStyle%3E%3C/UserStyle%3E%3C/NamedLayer%3E%3C/StyledLayerDescriptor%3E

  • Your sld seems fine, can you edit the question to show what it looks like after you call encodeURI() on it. – Ian Turton Dec 31 '16 at 11:29
  • Yeah, I edited question. – SaamTehraani Dec 31 '16 at 11:43
  • Where does the encode come from? because I think it should give you %3CCssParameter+name%3D%22fill%22%3E%23ff0000%3C%2FCssParameter%3E for the fill – Ian Turton Dec 31 '16 at 11:51
  • It is javascript built-in. I changed # to %23 but geoserver dont respond wms layer. – SaamTehraani Dec 31 '16 at 11:59
  • next thing to check is the geoserver log when you have turned logging up to developer – Ian Turton Dec 31 '16 at 11:59
5

You can Convert Hex value of color to base 10. for example use 4359924 (BASE 10) instead of #4286f4 (BASE 16) in CssParameter tag:

<CssParameter name="fill">4359924</CssParameter>
  • 1
    But why does it help? Shouldn't using hex value for color be OK by the standards? – user30184 Jan 1 '17 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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