Skip to content

Instantly share code, notes, and snippets.

@mrcnc
Created March 14, 2017 15:21
Show Gist options
  • Select an option

  • Save mrcnc/ce812d102e69a577fce32110692ee4c6 to your computer and use it in GitHub Desktop.

Select an option

Save mrcnc/ce812d102e69a577fce32110692ee4c6 to your computer and use it in GitHub Desktop.
trying to validate wfs-t requests using the schemas
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
public class Test {
// taken from the geoserver demo request: WFS_transactionInsert.xml
private static String tx =
"<wfs:Transaction service=\"WFS\" version=\"1.0.0\"\n" +
" xmlns:wfs=\"http://www.opengis.net/wfs\"\n" +
" xmlns:topp=\"http://www.openplans.org/topp\"\n" +
" xmlns:gml=\"http://www.opengis.net/gml\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd http://www.openplans.org/topp http://localhost:8080/geoserver/wfs/DescribeFeatureType?typename=topp:tasmania_roads\">\n" +
" <wfs:Insert>\n" +
" <topp:tasmania_roads>\n" +
" <topp:the_geom>\n" +
" <gml:MultiLineString srsName=\"http://www.opengis.net/gml/srs/epsg.xml#4326\">\n" +
" <gml:lineStringMember>\n" +
" <gml:LineString>\n" +
" <gml:coordinates decimal=\".\" cs=\",\" ts=\" \">\n" +
"494475.71056415,5433016.8189323 494982.70115662,5435041.95096618\n" +
" </gml:coordinates>\n" +
" </gml:LineString>\n" +
" </gml:lineStringMember>\n" +
" </gml:MultiLineString>\n" +
" </topp:the_geom>\n" +
" <topp:TYPE>alley</topp:TYPE>\n" +
" </topp:tasmania_roads>\n" +
" </wfs:Insert>\n" +
"</wfs:Transaction>";
public static void main(String[] args) {
try {
validateWFST(tx);
System.out.println("Schema was valid!");
}
catch (SAXException | IOException e) {
System.out.println("Schema was invalid b/c " + e.toString());
}
}
private static void validateWFST(String transaction) throws IOException, SAXException {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new URL("http://schemas.opengis.net/wfs/1.0.0/wfs.xsd"));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(transaction)));
// why does the validation fail?
// Schema was invalid b/c org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 26; cvc-complex-type.2.4.a: Invalid content was found starting with element 'topp:tasmania_roads'. One of '{"http://www.opengis.net/gml":_Feature}' is expected.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment