我正在尝试通过lxml和xsd (ogckml22.xsd)验证一些XML。这是离线发生的。我是通过直接打开/读取的方式读取文件的
对于记录,http://www.opengis.net/kml/2.2无效。
来自另一篇文章:(由于评论请求而澄清..)
from lxml import etree
import os
import sys
import StringIO
file=open('ogckml22.xsd')
data=file.read()
str=StringIO.StringIO(data)
try:
xmlschema_doc=etree.parse(data)
except IOError as ex:
print "oops {0}".format(ex.strerror)
except:
print "Unexpected error:", sys.exc_info()[0]
xmlschema=etree.XMLSchema(xmlschema_doc) 我得到的只是一个“连接被拒绝”。使用try/except,我得到的xmlschema_doc是未定义的。
File "<stdin>", line 1, in <module>
File "<xmlschema.pxi",line 105, in lxml.etree.XMLSchema.__init__ (src/lxml/lxml.etree.c:132748
self.error_log)
lxml.etree.XMLSchemaParseError: connection refused 我知道它可以读取上面的xsd文件和包含的另一个xsd文件。
好吧,也许xsd被读取了?我下载了lxml的源代码,并在src/lxml/xmlschema.pxi中,
if self._c_schema is NULL:
raise XMLSchemaParseError(
self.error_log._buildExceptionMessage(
u"Document is not valid XML Schema"),
self._error_log)我从未看到"Document is not valid XML Schema“消息。我只能假设使用“连接被拒绝”来代替“文档消息”(默认?)但是,对_error_log (除了重新编译之外)的更彻底的阅读使我难以理解……
由衷地,
ArrowInTree
发布于 2012-11-09 02:27:59
<!-- import atom:author and atom:link -->
<import namespace="http://www.w3.org/2005/Atom"
schemaLocation="atom-author-link.xsd"/>
<!-- import xAL:Address -->
<import namespace="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"
schemaLocation="http://docs.oasis-open.org/election/external/xAL.xsd"/>如果希望离线解析模式,则需要在本地提供这两个文档,并且schemaLocation提供的路径必须是正确的。
可以简化模式的解析和加载(不需要StringIO):
from lxml import etree
xmlschema_doc = etree.parse("ogckml22.xsd")
xmlschema = etree.XMLSchema(xmlschema_doc)
print xmlschema输出:
<lxml.etree.XMLSchema object at 0x00D25120>我不明白你说的“http://www.opengis.net/kml/2.2是无效的”是什么意思。
xmlschema_doc = etree.parse("http://www.opengis.net/kml/2.2")https://stackoverflow.com/questions/13262605
复制相似问题