Namespace handling fixes
[shibboleth/xmltooling.git] / xmltooling / io / AbstractXMLObjectMarshaller.cpp
index da5cf65..d5c4ac0 100644 (file)
@@ -161,7 +161,8 @@ DOMElement* AbstractXMLObjectMarshaller::marshall(XMLObject* xmlObject, DOMEleme
         \r
 void AbstractXMLObjectMarshaller::marshallInto(XMLObject& xmlObject, DOMElement* targetElement) const\r
 {\r
-    targetElement->setPrefix(xmlObject.getElementQName().getPrefix());\r
+    if (xmlObject.getElementQName().hasPrefix())\r
+        targetElement->setPrefix(xmlObject.getElementQName().getPrefix());\r
     marshallElementType(xmlObject, targetElement);\r
     marshallNamespaces(xmlObject, targetElement);\r
     marshallAttributes(xmlObject, targetElement);\r
@@ -242,8 +243,12 @@ public:
 \r
     const XMLCh* lookupNamespaceURI(const DOMNode* n, const XMLCh* prefix) const {\r
         // Return NULL if no declaration in effect. The empty string signifies the null namespace.\r
-        if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE)\r
+        if (!n || n->getNodeType()!=DOMNode::ELEMENT_NODE) {\r
+            // At the root, the default namespace is set to the null namespace.\r
+            if (!prefix || !*prefix)\r
+                return &chNull;\r
             return NULL;    // we're done\r
+        }\r
         DOMNamedNodeMap* attributes = static_cast<const DOMElement*>(n)->getAttributes();\r
         if (!attributes)\r
             return lookupNamespaceURI(n->getParentNode(),prefix);   // defer to parent\r
@@ -257,7 +262,7 @@ public:
             if (!XMLString::equals(attribute->getNamespaceURI(),XMLConstants::XMLNS_NS))\r
                 continue;   // not a namespace declaration\r
             // Local name should be the prefix and the value would be the URI, except for the default namespace.\r
-            if (!prefix && XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX))\r
+            if ((!prefix || !*prefix) && XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX))\r
                 return attribute->getNodeValue();\r
             else if (XMLString::equals(prefix,attribute->getLocalName()))\r
                 return attribute->getNodeValue();\r