2

What is the best way to validate SLD?

I'm using geoserver-manager but i cannot find any method to validate SLD. Even if i publish invalid SLD over geoserver-manager the response is 200 OK but it is not saved.

Of course I'm asking for soultion over geoserver rest api.

  • 1
    The SLD xsd file is published (and should be referenced from your SLD file) so any XML validation tool can be used. Maybe your editor has built in validation, the GeoServer SLD editor does. – Ian Turton Jun 3 '15 at 9:46
1

This is not what i wanted, but it works.

public void validate(InputStream xml, InputStream xsd) throws CustomException {

  try {

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new StreamSource(xsd));

    Validator validator = schema.newValidator();
    validator.validate(new StreamSource(xml));

  } catch (SAXException | IOException e) {

     throw new CustomException(e);
  } finally {
    try { xml.close(); } catch (IOException e) { log.error(e); }
    try { xsd.close(); } catch (IOException e) { log.error(e); }
  }

}

public void validate(String xml, String xsd) throws CustomException {
  xsd = xsd.trim();
  validate(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)),
    new ByteArrayInputStream(xsd.getBytes(StandardCharsets.UTF_8)));
}

public void validate(String xml, URL xsdUrl) throws CustomException {
  try {
    validate(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)), xsdUrl.openStream());
  } catch (IOException e) {
    log.error(e);
    throw new CustomException(e);
  }
}

I'm using validation with URL beacuse of imports in StyledLayerDescriptor schema:

xmlValidator.validate(sld, new URL("http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd"));

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.