首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python lxml离线验证会导致“连接被拒绝”到http://www.opengis.net/kml/2.2

Python lxml离线验证会导致“连接被拒绝”到http://www.opengis.net/kml/2.2
EN

Stack Overflow用户
提问于 2012-11-07 11:10:20
回答 1查看 874关注 0票数 0

我正在尝试通过lxml和xsd (ogckml22.xsd)验证一些XML。这是离线发生的。我是通过直接打开/读取的方式读取文件的

对于记录,http://www.opengis.net/kml/2.2无效。

来自另一篇文章:(由于评论请求而澄清..)

代码语言:javascript
复制
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是未定义的。

代码语言:javascript
复制
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中,

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-09 02:27:59

代码语言:javascript
复制
<!-- 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):

代码语言:javascript
复制
from lxml import etree

xmlschema_doc = etree.parse("ogckml22.xsd") 
xmlschema = etree.XMLSchema(xmlschema_doc)

print xmlschema

输出:

代码语言:javascript
复制
<lxml.etree.XMLSchema object at 0x00D25120>

我不明白你说的“http://www.opengis.net/kml/2.2是无效的”是什么意思。

代码语言:javascript
复制
xmlschema_doc = etree.parse("http://www.opengis.net/kml/2.2")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13262605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档