The DTD can be external, but you have to call setName: on it so that it is
coordinated with the root of your XML document. For example, suppose your
xml goes like this (ripped off the Internet, silly example):

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

And the dtd goes like this:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

Then this works:

NSURL* xmlurl = [[NSBundle mainBundle] URLForResource:@"xml"
withExtension:@"xml"];
NSXMLDocument* doc = [[NSXMLDocument alloc] initWithContentsOfURL:xmlurl
options:0 error:nil];
NSURL* dtdurl = [[NSBundle mainBundle] URLForResource:@"dtd"
withExtension:@"dtd"];
NSXMLDTD* dtd = [[NSXMLDTD alloc] initWithContentsOfURL:dtdurl options:0
error:nil];
[dtd setName:@"note"];
[doc setDTD:dtd];
BOOL valid = [doc validateAndReturnError:nil];
if (valid) NSLog(@"valid"); // "valid"

This point was well covered in a previous thread:

http://lists.apple.com/archives/cocoa-dev/2006/Sep/msg00464.html

m.


> Date: Tue, 19 Jan 2010 20:02:23 -0800
> From: Rainer Standke <li...@standke.com>
> Subject: Validating NSXMLDocument against external DTD
> 
> Hello,
> 
> I am trying to validate an NSXMLDocument against an external DTD. Here
> is what I do:
> 
>     NSError *error = nil;
> 
>      NSXMLDocument *doc = [[[NSXMLDocument alloc]
> initWithXMLString:beamedXML options:NSXMLNodeOptionsNone error:&error]
> autorelease];
>      if (!doc) {
>          NSLog(@"error: %@", error);
>      }
> 
> NSXMLElement *root = [doc rootElement];
> //NSLog(@"%@", [root description]);
> 
> 
> //NSBundle *theBundle = [NSBundle mainBundle];
> //NSLog(@"%@", [theBundle description]);
> 
> NSString *dtdPath = [[NSBundle mainBundle]
> pathForResource:@"FCPXMLv5" ofType:@"dtd"];
> NSLog(@"dtdPath: %@", dtdPath);
> 
> NSURL *dtdUrl = [NSURL fileURLWithPath:dtdPath isDirectory:NO];
> 
> error = nil;
> NSXMLDTD *theDtd = [[[NSXMLDTD alloc] initWithContentsOfURL:dtdUrl
> options:NSXMLNodePreserveWhitespace error:&error] autorelease];
> if (!theDtd) {
>          NSLog(@"error: %@", error);
>      }
> //NSLog(@"theDtd: %@", theDtd);
> 
> [doc setDTD:theDtd];
> 
> error = nil;
> if (! [doc validateAndReturnError:&error]) {
> NSLog(@"error: %@", error);
> }
> 
> And this is what I get:
> 
> Error Domain=NSXMLParserErrorDomain Code=1 UserInfo=0xdc17a50 "no DTD
> found!
> 
> It seems to me that the DTD is expected to be found inside the XML
> document. (The DTD seems to be created alright, since I can log it's
> description.) In this case that not what I need or want. The Tree-
> Based XML Programming Guide for Cocoa talks about external DTDs but I
> can't find a clue as how to do this. Is it even possible?

-- 
matt neuburg, phd = m...@tidbits.com, http://www.tidbits.com/matt/
pantes anthropoi tou eidenai oregontai phusei
Among the 2007 MacTech Top 25, http://tinyurl.com/2rh4pf
AppleScript: the Definitive Guide, 2nd edition
http://www.tidbits.com/matt/default.html#applescriptthings
Take Control of Exploring & Customizing Snow Leopard
http://tinyurl.com/kufyy8
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to