带有wfs的xml:transaction / insert-request无法针对WFS xsd-schema进行验证

时间:2012-11-15 16:39:48

标签: xml xsd openlayers gml-geographic-markup-lan

我试图解决的问题与OpenLayers和TinyOWS之间的交互有关,即插入新功能,但我不会在此发布任何Javascript代码或TinyOWS配置,因为它们与xml验证问题无关。我试图尽可能地隔离问题。下面是使用lxml库对模式进行XML验证的python2代码:

from lxml import etree
from StringIO import StringIO

parser = etree.XMLParser(no_network=False)
etree.set_default_parser(parser)

schema_doc = etree.parse(StringIO('''\
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" xmlns:tows="http://www.tinyows.org/" targetNamespace="http://www.tinyows.org/" elementFormDefault="qualified" version="1.1">
<xs:import namespace="http://www.opengis.net/wfs" schemaLocation="http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"/>
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/3.1.1/base/gml.xsd"/>
<xs:element name="cities" type="tows:citiesType" substitutionGroup="gml:_Feature"/>
<xs:complexType name="citiesType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="type_id" type="int" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="properties" type="string" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="geom" type="gml:PointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
'''))

xml = etree.parse(StringIO('''\
<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs">
<wfs:Insert>
<feature:cities xmlns:feature="http://www.tinyows.org">
<feature:geom>
<gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
<gml:pos>37.935083007812 55.4423828125</gml:pos>
</gml:Point>
</feature:geom>
</feature:cities>
</wfs:Insert>
</wfs:Transaction>'''))

schema = etree.XMLSchema(schema_doc)
schema.assertValid(xml)

我收到错误:

lxml.etree.DocumentInvalid: Element '{http://www.tinyows.org}cities': This element is  not expected. Expected is one of ( {http://www.opengis.net/gml}_Feature, http://www.opengis.net/gml}FeatureCollection, {http://www.opengis.net/gml}MultiPointCoverage, {http://www.opengis.net/gml}MultiCurveCoverage, {http://www.opengis.net/gml}MultiSurfaceCoverage, {http://www.opengis.net/gml}MultiSolidCoverage, {http://www.opengis.net/gml}GridCoverage, {http://www.opengis.net/gml}RectifiedGridCoverage, {http://www.opengis.net/gml}Observation, {http://www.opengis.net/gml}DirectedObservation )., line 3

这对我来说似乎很神秘,因为cities功能被定义为替代架构中的gml:_Feature。也许答案应该在http://schemas.opengis.net/wfs/1.1.0/wfs.xsd内,但我无法理解验证的逻辑。

1 个答案:

答案 0 :(得分:0)

我发现字符串<feature:cities xmlns:feature="http://www.tinyows.org">是所有邪恶的根源,即在http://www.tinyows.org命名空间中缺少尾部斜杠。将其更改为<feature:cities xmlns:feature="http://www.tinyows.org/">解决了问题。每个符号都很重要!