Index: src/OFXMLElement.m
==================================================================
--- src/OFXMLElement.m
+++ src/OFXMLElement.m
@@ -239,23 +239,20 @@
[all_namespaces setObject: obj
forKey: key];
} else
all_namespaces = namespaces;
+ prefix = [all_namespaces objectForKey: (ns != nil ? ns : @"")];
+
i = 0;
len = [name cStringLength] + 3;
str_c = [self allocMemoryWithSize: len];
/* Start of tag */
str_c[i++] = '<';
- if ((ns == nil && def_ns != nil) || (ns != nil && def_ns == nil) ||
- (ns != nil && ![ns isEqual: def_ns])) {
- if ((prefix = [all_namespaces objectForKey:
- (ns != nil ? ns : (OFString*)@"")]) == nil)
- @throw [OFUnboundNamespaceException newWithClass: isa
- namespace: ns];
+ if (prefix != nil && ![ns isEqual: def_ns]) {
len += [prefix cStringLength] + 1;
@try {
str_c = [self resizeMemory: str_c
toSize: len];
} @catch (id e) {
@@ -269,10 +266,31 @@
str_c[i++] = ':';
}
memcpy(str_c + i, [name cString], [name cStringLength]);
i += [name cStringLength];
+
+ /* xmlns if necessary */
+ if (ns != nil && prefix == nil && ![ns isEqual: def_ns]) {
+ len += [ns cStringLength] + 9;
+
+ @try {
+ str_c = [self resizeMemory: str_c
+ toSize: len];
+ } @catch (id e) {
+ [self freeMemory: str_c];
+ @throw e;
+ }
+
+ memcpy(str_c + i, " xmlns='", 8);
+ i += 8;
+ memcpy(str_c + i, [ns cString], [ns cStringLength]);
+ i += [ns cStringLength];
+ str_c[i++] = '\'';
+
+ def_ns = ns;
+ }
/* Attributes */
attrs_carray = [attributes cArray];
attrs_count = [attributes count];
@@ -333,11 +351,11 @@
for (j = 0; j < children_count; j++)
append(tmp, @selector(
appendCStringWithoutUTF8Checking:),
[[children_carray[j]
_stringValueWithParentNamespaces: all_namespaces
- parentDefaultNamespace: defaultNamespace] cString]);
+ parentDefaultNamespace: def_ns] cString]);
len += [tmp cStringLength] + [name cStringLength] + 2;
@try {
str_c = [self resizeMemory: str_c
toSize: len];
Index: tests/OFXMLElementTests.m
==================================================================
--- tests/OFXMLElementTests.m
+++ tests/OFXMLElementTests.m
@@ -46,11 +46,17 @@
namespace: @"urn:objfw:test"]) &&
R([elem[2] addAttributeWithName: @"test"
stringValue: @"test"]) &&
R([elem[2] setPrefix: @"objfw-test"
forNamespace: @"urn:objfw:test"]) &&
- [[elem[2] stringValue] isEqual: @""])
+ [[elem[2] stringValue] isEqual: @""] &&
+ (elem[3] = [OFXMLElement elementWithName: @"foo"
+ namespace: @"urn:objfw:test"]) &&
+ R([elem[3] addAttributeWithName: @"test"
+ stringValue: @"test"]) &&
+ [[elem[3] stringValue] isEqual:
+ @""])
TEST(@"+[elementWithName:namespace:stringValue:]",
(elem[3] = [OFXMLElement elementWithName: @"foo"
namespace: @"urn:objfw:test"
stringValue: @"x"]) &&