This logic came from playing with the wfs-states example data. But I didn't check if the GeoServer output conforms to the spec.