if (NS_FAILED(AppendChild(aElement, child))) {
if (NS_FAILED(AppendChild(containingNameSpace, aElement, child))) {
RDFXULBuilderImpl::CreateContents(nsIContent* aElement){    nsresult rv;    // See if someone has marked the element's contents as being    // generated: this prevents a re-entrant call from triggering    // another generation.    nsAutoString attrValue;    if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,                                              kXULContentsGeneratedAtom,                                              attrValue))) {        NS_ERROR("unable to test contents-generated attribute");        return rv;    }    if ((rv == NS_CONTENT_ATTR_HAS_VALUE) && (attrValue.EqualsIgnoreCase("true")))        return NS_OK;    // Get the treeitem's resource so that we can generate cell    // values. We could QI for the nsIRDFResource here, but doing this    // via the nsIContent interface allows us to support generic nodes    // that might get added in by DOM calls.    nsCOMPtr<nsIRDFResource> resource;    if (NS_FAILED(rv = GetElementResource(aElement, getter_AddRefs(resource)))) {        NS_ERROR("unable to get element resource");        return rv;    }    // Ignore any elements that aren't XUL elements: we can't    // construct content for them anyway.    PRBool isXULElement;    if (NS_FAILED(rv = mDB->HasAssertion(resource, kRDF_instanceOf, kXUL_element, PR_TRUE, &isXULElement))) {        NS_ERROR("unable to determine if element is a XUL element");        return rv;    }    if (! isXULElement)        return NS_OK;    // If it's a XUL element, it'd better be an RDF Sequence...    NS_ASSERTION(rdf_IsContainer(mDB, resource), "element is a XUL:element, but not an RDF:Seq");    if (! rdf_IsContainer(mDB, resource))        return NS_ERROR_UNEXPECTED;    // Now mark the element's contents as being generated so that    // any re-entrant calls don't trigger an infinite recursion.    if (NS_FAILED(rv = aElement->SetAttribute(kNameSpaceID_None,                                              kXULContentsGeneratedAtom,                                              "true",                                              PR_FALSE))) {        NS_ERROR("unable to set contents-generated attribute");        return rv;    }    // Iterate through all of the element's children, and construct    // appropriate children for each arc.    nsCOMPtr<nsIRDFAssertionCursor> children;    if (NS_FAILED(rv = NS_NewContainerCursor(mDB, resource, getter_AddRefs(children)))) {        NS_ERROR("unable to create cursor for children");        return rv;    }    while (1) {        rv = children->Advance();        if (NS_FAILED(rv))            return rv;        if (rv == NS_RDF_CURSOR_EMPTY)            break;        nsCOMPtr<nsIRDFNode> child;        if (NS_FAILED(rv = children->GetTarget(getter_AddRefs(child)))) {            NS_ERROR("error reading cursor");            return rv;        }        NS_ASSERTION(rv != NS_RDF_NO_VALUE, "null value in cursor");        if (rv == NS_RDF_NO_VALUE)            continue;        if (NS_FAILED(AppendChild(aElement, child))) {            NS_ERROR("problem appending child to content model");            return rv;        }    }    // Now that we've built the children, check to see if the includesrc attribute    // exists on the node.    nsString includeSrc;    if (NS_FAILED(rv = aElement->GetAttribute(kNameSpaceID_None,                                              kXULIncludeSrcAtom,                                              includeSrc))) {        NS_ERROR("unable to retrieve includeSrc attribute");        return rv;    }    if (rv == NS_CONTENT_ATTR_HAS_VALUE) {        // Build a URL object from the attribute's value.                nsCOMPtr<nsIDocument> parentDoc;        aElement->GetDocument(*getter_AddRefs(parentDoc));        if (parentDoc == nsnull) {            NS_ERROR("Unable to retrieve parent document for a subdocument.");            return rv;        }            nsCOMPtr<nsIContentViewerContainer> container;        nsCOMPtr<nsIXULParentDocument> xulParentDocument;        xulParentDocument = do_QueryInterface(parentDoc);        if (xulParentDocument == nsnull) {            NS_ERROR("Unable to turn document into a XUL parent document.");            return rv;        }        if (NS_FAILED(rv = xulParentDocument->GetContentViewerContainer(getter_AddRefs(container)))) {            NS_ERROR("Unable to retrieve content viewer container from parent document.");            return rv;        }        nsAutoString command;        if (NS_FAILED(rv = xulParentDocument->GetCommand(command))) {            NS_ERROR("Unable to retrieve the command from parent document.");            return rv;        }                nsCOMPtr<nsIDOMXULElement> xulElement;        xulElement = do_QueryInterface(aElement);        if (!xulElement) {            NS_ERROR("The fragment root is not a XUL element.");            return rv;        }        nsCOMPtr<nsIRDFResource> rdfResource;        xulElement->GetResource(getter_AddRefs(rdfResource));        if (!rdfResource) {            NS_ERROR("The fragment root doesn't have an RDF resource behind it.");            return rv;        }                nsCOMPtr<nsIXULDocumentInfo> docInfo;        if (NS_FAILED(rv = nsComponentManager::CreateInstance(kXULDocumentInfoCID,                                                              nsnull,                                                              kIXULDocumentInfoIID,                                                              (void**) getter_AddRefs(docInfo)))) {            NS_ERROR("unable to create document info object");            return rv;        }                if (NS_FAILED(rv = docInfo->Init(parentDoc, rdfResource))) {            NS_ERROR("unable to initialize doc info object.");            return rv;        }                // Turn the content viewer into a webshell        nsCOMPtr<nsIWebShell> webshell;        webshell = do_QueryInterface(container);        if (webshell == nsnull) {            NS_ERROR("this isn't a webshell. we're in trouble.");            return rv;        }        nsCOMPtr<nsIDocumentLoader> docLoader;        if (NS_FAILED(rv = webshell->GetDocumentLoader(*getter_AddRefs(docLoader)))) {            NS_ERROR("unable to obtain the document loader to kick off the load.");            return rv;        }        docLoader->LoadSubDocument(includeSrc,                                   docInfo.get());    }    return rv;}