NSXMLDocument validateAndReturnError 需要网络连接吗?

发布于 2024-10-19 15:47:40 字数 1753 浏览 4 评论 0原文

我正在尝试根据架构的架构验证 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-10-26 15:47:40

查看Apple文档中的 NSXMLDTD 类表明您确实有权访问目录。然而,对定义要使用的目录的支持似乎非常有限。您可以在 /etc/xml/catalog 中创建目录或设置 XML_CATALOG_FILES 环境变量。

获得目录后,您应该能够在其中放置 W3 架构的条目以及本地副本。类似于:

<?xml version="1.0"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" 
    "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
  <system systemId="http://www.w3.org/2001/xml.xsd" uri="xml.xsd"/>  
  <uri name="http://www.w3.org/XML/1998/namespace" uri="xml.xsd"/>
</catalog>

这假设您的目录和架构位于同一目录中。

我自己不能尝试这个(缺乏时间和生锈的可可技能),但它应该有效。如果我没记错的话,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 the XML_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:

<?xml version="1.0"?>
<!DOCTYPE catalog PUBLIC "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN" 
    "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd">
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
  <system systemId="http://www.w3.org/2001/xml.xsd" uri="xml.xsd"/>  
  <uri name="http://www.w3.org/XML/1998/namespace" uri="xml.xsd"/>
</catalog>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文