shithub: xml-9atom

ref: 4e09200f7e4bf8791c9c720922734e0759206b72
dir: /libxml/xmlns.c/

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

Ns*
xmladdns(Xml *xml, char *name, char *decl)
{
	Ns *n;

	if (!xml->ns) {
		xml->ns = xmlcalloc(xml, sizeof(Ns), 1);
		n = xml->ns;
		if (name)
			n->name = xmlstrdup(xml, name, 1);
		if (decl)
			n->decl = xmlstrdup(xml, decl, 0);
		goto Out;
	}

	n = xml->ns;
	while (n->next)
		n = n->next;

	n->next = xmlcalloc(xml, sizeof(Ns), 1);
	n = n->next;
	if (name)
		n->name = xmlstrdup(xml, name, 1);
	if (decl)
		n->decl = xmlstrdup(xml, decl, 0);
Out:
	if (xmldebug == 1)
		fprint(2, "addns: %s = %s\n", name, decl);
	return n;
}

Ns*
xmlfindns(Xml *xml, char *name)
{
	Ns *n;
	int i;
	char *s;

	if (!name) {
		i = 0;
		goto Search;
	}

	s = strchr(name, ':');
	if (!s) {
		i = strlen(name);
		goto Search;
	}
	i = s - name;

Search:
	if (xmldebug) {
		if (i)
			fprint(2, "search for xmlns %.*s (%d)\n", i, name, i);
		else
			fprint(2, "search for root xmlns\n");
	}
	for (n = xml->ns; n; n = n->next) {
		if (n->name == nil && !i)
			return n;
		if (n->name && i && strncmp(n->name, name, i) == 0)
			return n;
	}
	if (!i)
		return xmladdns(xml, nil, nil);
	s = mallocz(i+1, 1);
	strncpy(s, name, i);
	n = xmladdns(xml, s, nil);
	free(s);
	return n;
}