I have the following NSXMLDocument: <document xmlns="http://example.com/document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </document>
I want to add an xsi:schemaLocation to the document so that I can validate it. The documentation says: attributeWithName:URI:stringValue: Returns an NSXMLNode object representing an attribute node with a given qualified name and string. + (id)attributeWithName:(NSString *)name URI:(NSString *)URI stringValue:(NSString *)value Parameters name A string that is the name of an attribute. URI A URI (Universal Resource Identifier) that qualifies name. value A string that is the value of the attribute. Return Value An NSXMLNode object of kind NSXMLAttributeKind or nil if the object couldn't be created. Discussion For example, in the attribute “bst:id=`12345’”, “bst” is the name qualifier (derived from the URI), “id” is the attribute name, and “12345” is the attribute value. Thus, I do the following: NSXMLElement rootXmlElement = ... NSXMLNode *schemaLocationAttributeXmlNode = [NSXMLNode attributeWithName:@"schemaLocation" URI:@"xsi" stringValue:@"http://example.com/document document.xsd"]; [rootXmlElement addAttribute:schemaLocationAttributeXmlNode]; However, this yields the following xml: <document xmlns="http://example.com/document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation=@"http://example.com/document document.xsd"> </document> Notice that there should be an "xsi" prefix on the "schemaLocation" attribute. and if I call try to get the attribute from the rootXmlElement, I get nothing: [rootXmlElement attributeForLocalName:@"schemaLocation" URI:@"http://www.w3.org/2001/XMLSchema-instance"] == nil Since this didn't work, I tried using the namespace URI instead of the prefix when creating the attribute: NSXMLElement rootXmlElement = ... NSXMLNode *schemaLocationAttributeXmlNode = [NSXMLNode attributeWithName:@"schemaLocation" URI:@"http://www.w3.org/2001/XMLSchema-instance" stringValue:@"http://example.com/document document.xsd"]; [rootXmlElement addAttribute:schemaLocationAttributeXmlNode]; This still yielded bad xml: <document xmlns="http://example.com/document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" schemaLocation=@"http://example.com/document document.xsd"> </document> However, this time I can at least get the attribute from my rootXmlElement: [rootXmlElement attributeForLocalName:@"schemaLocation" URI:@"http://www.w3.org/2001/XMLSchema-instance"] != nil // closer! So, now, I tried specifying the prefix in the attribute name when creating the attribute: NSXMLElement rootXmlElement = ... NSXMLNode *schemaLocationAttributeXmlNode = [NSXMLNode attributeWithName:@"xsi:schemaLocation" URI:@"http://www.w3.org/2001/XMLSchema-instance" stringValue:@"http://example.com/document document.xsd"]; [rootXmlElement addAttribute:schemaLocationAttributeXmlNode]; Success! I got good xml: <document xmlns="http://example.com/document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=@"http://example.com/document document.xsd"> </document> This doesn't seem consistent with the documentation. Am I hacking here? Thanks! -Heath Borders [email protected] Twitter: heathborders http://heath-tech.blogspot.com _______________________________________________ Cocoa-dev mailing list ([email protected]) 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 [email protected]
