shithub: xml-9atom

ref: f1e559d47916b74f473153a0a04df6956ca4052b
dir: /libxml/xmlattr.c/

View raw version
#include <u.h>
#include <libc.h>
#include "xml.h"

Attr *
xmlattr(Xml *xp, Attr **root, Elem *parent, char *name, char *value)
{
	Attr *ap, *t;
	char *s;
	Ns *n;

	if((ap = xmlcalloc(xp, sizeof(Attr), 1)) == nil)
		sysfatal("no memory - %r\n");
	if(*root == nil){
		*root = ap;
	}
	else{
		for (t = *root; t->next; t = t->next)
			continue;
		t->next = ap;
	}
	ap->parent = parent;

	if (name) {
		n = nil;
		if (strncmp(name, "xmlns:", 6) == 0) {
			n = xmlfindns(xp, name+6);
		} else
		if (strcmp(name, "xmlns") == 0) {
			n = xmlfindns(xp, nil);
		}
		if (n && value)
			n->decl = xmlstrdup(xp, value, 0);
	}

	if(name){
		s = strchr(name, ':');
		if (s) {
			ap->ns = xmlfindns(xp, name);
			s++;
		} else
			s = name;
		if((ap->name = xmlstrdup(xp, s, 1)) == nil)
			sysfatal("no memory - %r\n");
	}

	if(value)
		if((ap->value = xmlstrdup(xp, value, 0)) == nil)
			sysfatal("no memory - %r\n");
	return ap;
}