ref: f1e559d47916b74f473153a0a04df6956ca4052b
dir: /libxpath/fns.c/
#include <u.h> #include <libc.h> #include <xml.h> #include <xpath.h> #include "dat.h" int position(Elem *e) { Elem *p; p = e->parent; int i; i = 0; for (p = p->child; p; p = p->next) { if (strcmp(p->name, e->name) == 0) i++; if (p == e) return i; } return i; } int last(Elem *e) { Elem *p; p = e->parent; int i; i = 0; for (p = p->child; p; p = p->next) { if (strcmp(p->name, e->name) == 0) i++; } return i; } void ftext(XpResult *r, Elem *ep) { buildsinglestring(r, ep->pcdata); } void fposition(XpResult *r, Elem *ep) { buildsinglenum(r, position(ep)); } void fprocinst(XpResult *r, Elem *ep) { fprint(2, "function processing-instruction()"); } void fcomment(XpResult *r, Elem *ep) { fprint(2, "function comment()"); } void fnode(XpResult *r, Elem *ep) { buildsingleelem(r, ep); } void flast(XpResult *r, Elem *ep) { buildsinglenum(r, last(ep)); } void initfuncs() { #define F(name, func) addfunc(addname(name), func) F("text", ftext); F("position", fposition); F("processing-instruction", fprocinst); F("comment", fcomment); F("node", fnode); F("last", flast); #undef F }