if (tag.get() != kTemplateAtom) continue;
PRInt32 nameSpaceID; rv = child->GetNameSpaceID(nameSpaceID); if (NS_FAILED(rv)) return rv;
RDFGenericBuilderImpl::FindTemplate(nsIContent* aElement,                                    nsIRDFResource *aProperty,                                    nsIRDFResource* aChild,                                    nsIContent **aTemplate){	nsresult		rv;	*aTemplate = nsnull;	nsCOMPtr<nsIContent>	tmpl;	PRBool			foundTemplateFlag = PR_FALSE;	nsAutoString	templateID;	if (NS_SUCCEEDED(rv = mRoot->GetAttribute(kNameSpaceID_None, kTemplateAtom, templateID))		&& (rv == NS_CONTENT_ATTR_HAS_VALUE))	{		// first, check and see if the root has a template attribute		nsCOMPtr<nsIDOMXULDocument>	xulDoc;		xulDoc = do_QueryInterface(mDocument);                if (! xulDoc)	return(NS_ERROR_UNEXPECTED);		nsCOMPtr<nsIDOMElement>		domElement;                rv = xulDoc->GetElementById(templateID, getter_AddRefs(domElement));                NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find template node");                if (NS_FAILED(rv))	return(rv);		tmpl = do_QueryInterface(domElement);		if (tmpl)	foundTemplateFlag = PR_TRUE;	}	if (foundTemplateFlag == PR_FALSE)	{		// if root node has no template attribute, then		// look for a child node which is a template tag		PRInt32	count;		rv = mRoot->ChildCount(count);		if (NS_FAILED(rv)) return rv;		for (PRInt32 loop=0; loop<count; loop++)		{			rv = mRoot->ChildAt(loop, *getter_AddRefs(tmpl));			if (NS_FAILED(rv)) return rv;			PRInt32 nameSpaceID;			rv = tmpl->GetNameSpaceID(nameSpaceID);			if (NS_FAILED(rv)) return rv;			if (nameSpaceID != kNameSpaceID_XUL)				continue;			nsCOMPtr<nsIAtom>	tag;			rv = tmpl->GetTag(*getter_AddRefs(tag));			if (NS_FAILED(rv)) return rv;			if (tag.get() != kTemplateAtom)				continue;			foundTemplateFlag = PR_TRUE;			break;		}	}	if (foundTemplateFlag == PR_TRUE)	{		// found a template; check against any (optional) rules		PRInt32		numRuleChildren, numRulesFound = 0;		rv = tmpl->ChildCount(numRuleChildren);		if (NS_FAILED(rv)) return rv;		for (PRInt32 ruleLoop=0; ruleLoop<numRuleChildren; ruleLoop++)		{			nsCOMPtr<nsIContent>	aRule;			rv = tmpl->ChildAt(ruleLoop, *getter_AddRefs(aRule));			if (NS_FAILED(rv)) return rv;			PRInt32	ruleNameSpaceID;			rv = aRule->GetNameSpaceID(ruleNameSpaceID);			if (NS_FAILED(rv)) return rv;			if (ruleNameSpaceID != kNameSpaceID_XUL)				continue;			nsCOMPtr<nsIAtom>	ruleTag;			rv = aRule->GetTag(*getter_AddRefs(ruleTag));			if (NS_FAILED(rv)) return rv;			if (ruleTag.get() == kRuleAtom)			{				++numRulesFound;				PRBool		isMatch = PR_FALSE;				rv = IsTemplateRuleMatch(aElement, aProperty, aChild, aRule, &isMatch);				if (NS_FAILED(rv)) return rv;				if (isMatch)				{					// found a matching rule, use it as the template					*aTemplate = aRule;					NS_ADDREF(*aTemplate);					return NS_OK;				}			}		}		if (numRulesFound == 0)		{			// if no rules are specified in the template, just use it			*aTemplate = tmpl;			NS_ADDREF(*aTemplate);			return NS_OK;		}	}	return(NS_ERROR_FAILURE);}