NSXMLDocument validateAndReturnError 需要网络连接吗?
我正在尝试根据架构的架构验证 XML 架构文档 (http://www.w3.org/ 2001/XMLSchema)使用 NSXMLDocument。我已经让它正常工作,并假设我正在根据本地模式进行验证。
然而,我发现如果没有网络连接,这个验证就不起作用。有没有办法强制 NSXMLDocument 使用本地模式进行验证?
我使用网络连接的代码:
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:(NSXMLDocumentValidate | NSXMLNodePreserveAll)
error:&err];
NSXMLElement *rootElement = [xmlDoc rootElement];
NSMutableArray *namespaces = [[rootElement namespaces] mutableCopy];
[namespaces addObject:[NSXMLNode namespaceWithName:@"xsi" stringValue:@"http://www.w3.org/2001/XMLSchema-instance"]];
[rootElement setNamespaces:namespaces];
[rootElement removeAttributeForName:@"xsi:schemaLocation"];
[rootElement addAttribute:[NSXMLNode attributeWithName:@"xsi:schemaLocation" stringValue:[NSString stringWithFormat:@"http://www.w3.org/2001/XMLSchema %@", @"/System/Library/Schemas/XMLSchema.xsd"]]];
BOOL vaildXML = [xmlDoc validateAndReturnError:&err];
我正在验证的文档的架构标签:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:myCompany="http://schema.myCompany.com/SomeSchema"
targetNamespace="http://schema.myCompany.com/SomeSchema">
w3.org 架构位置似乎有问题,但不是我公司的。
我看到的错误
:没有这样的文件或目录 I/O 警告:无法加载外部实体“http://www.w3.org/2001/xml.xsd”
错误域=NSXMLParserErrorDomain Code=1 UserInfo=0x103051c10“元素'{http://www.w3.org /2001/XMLSchema}import':无法在位置“http://www.w3.org/2001/xml.xsd”找到架构,跳过导入。 属性使用(未知),属性“ref”:QName 值“{http://www.w3.org/XML/1998/namespace}lang”未解析为 (n) 属性声明。 属性使用(未知),属性“ref”:QName 值“{http://www.w3.org/XML/1998/namespace}lang”未解析为 (n) 属性声明。
有什么想法吗?
I am trying to validate an XML Schema document against the schema for schemas (http://www.w3.org/2001/XMLSchema) using NSXMLDocument. I've gotten it to work correctly, and assumed that I was validating against a local schema.
However, I discovered that without a network connection, this validation doesn't work. Is there any way to force NSXMLDocument to use a local schema for validation?
The code I have working with a net connection:
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:(NSXMLDocumentValidate | NSXMLNodePreserveAll)
error:&err];
NSXMLElement *rootElement = [xmlDoc rootElement];
NSMutableArray *namespaces = [[rootElement namespaces] mutableCopy];
[namespaces addObject:[NSXMLNode namespaceWithName:@"xsi" stringValue:@"http://www.w3.org/2001/XMLSchema-instance"]];
[rootElement setNamespaces:namespaces];
[rootElement removeAttributeForName:@"xsi:schemaLocation"];
[rootElement addAttribute:[NSXMLNode attributeWithName:@"xsi:schemaLocation" stringValue:[NSString stringWithFormat:@"http://www.w3.org/2001/XMLSchema %@", @"/System/Library/Schemas/XMLSchema.xsd"]]];
BOOL vaildXML = [xmlDoc validateAndReturnError:&err];
The schema tag of the document I'm validating:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:myCompany="http://schema.myCompany.com/SomeSchema"
targetNamespace="http://schema.myCompany.com/SomeSchema">
It seems to be having a problem with the w3.org schema location, but not my company's.
The error I'm seeing
error : No such file or directory
I/O warning : failed to load external entity "http://www.w3.org/2001/xml.xsd"
Error Domain=NSXMLParserErrorDomain Code=1 UserInfo=0x103051c10 "Element '{http://www.w3.org/2001/XMLSchema}import': Failed to locate a schema at location 'http://www.w3.org/2001/xml.xsd'. Skipping the import.
attribute use (unknown), attribute 'ref': The QName value '{http://www.w3.org/XML/1998/namespace}lang' does not resolve to a(n) attribute declaration.
attribute use (unknown), attribute 'ref': The QName value '{http://www.w3.org/XML/1998/namespace}lang' does not resolve to a(n) attribute declaration.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看Apple文档中的 NSXMLDTD 类表明您确实有权访问目录。然而,对定义要使用的目录的支持似乎非常有限。您可以在
/etc/xml/catalog
中创建目录或设置XML_CATALOG_FILES
环境变量。获得目录后,您应该能够在其中放置 W3 架构的条目以及本地副本。类似于:
这假设您的目录和架构位于同一目录中。
我自己不能尝试这个(缺乏时间和生锈的可可技能),但它应该有效。如果我没记错的话,NSXML 基于 libxml2,它当然支持目录。
目录本身的规范可以在 OASIS 网站 上找到。
Looking at the Apple docs for the NSXMLDTD class suggests that you do have access to catalogs. However, there seems to be very limited support for defining the catalog to be used. Either you can create a catalog at
/etc/xml/catalog
or set theXML_CATALOG_FILES
environment variable.Once you've got a catalog, you should be able to place an entry into it for the W3 schema along with a local copy. Something like:
This assumes that you have the catalog and the schema in the same directory.
I can't try this myself (lack of time and rusty Cocoa skills) but it should work. If I remember correctly, NSXML is based on libxml2 which certainly supports catalogs.
The specifications for catalogs themselves can be found on the OASIS website.