XMLAPI XML * xml_search (XML * start, const char * element, const char * attr, const char * value)
{
int found = 0;
XML * cur = start;
XML * next = NULL;
if (!xml_is_element (start)) return NULL;
while (!found) {
found = 1;
if (element) {
if (!xml_is (cur, element)) found = 0;
}
if (found && attr) {
if (value) {
if (strcmp (value, xml_attrval (cur, attr))) found = 0;
}
else if (!*xml_attrval (cur, attr)) found = 0;
}
if (found) return cur;
next = xml_firstelem (cur); /* TODO: Make this an xml_nextdepthfirst. */
while (!next) {
if (!next) next = xml_nextelem (cur);
if (!next) {
/*if (cur == start || !cur) return NULL;*/
cur = xml_parent (cur);
if (cur == start || !cur) return NULL;
}
}
cur = next;
}
}
XMLAPI XML * xml_searchf (XML * start, const char * element, const char * attr, const char * format, ...);
XMLAPI XML * xml_search_next (XML * top, XML * start, const char * element, const char * attr, const char * value)
{
int found = 0;
XML * cur = start;
XML * next;
if (!xml_is_element (start)) return NULL;
while (!found) {
next = xml_firstelem (cur);
while (!next) {
if (!next) next = xml_nextelem (cur);
if (!next) {
if (cur == top || !cur) return NULL;
cur = xml_parent (cur);
}
}
cur = next;
found = 1;
if (element) {
if (!xml_is (cur, element)) found = 0;
}
if (found && attr) {
if (value) {
if (strcmp (value, xml_attrval (cur, attr))) found = 0;
}
else if (!*xml_attrval (cur, attr)) found = 0;
}
if (found) return cur;
}
}
XMLAPI XML * xml_searchf_next (XML * top, XML * start, const char * element, const char * attr, const char * format, ...);
|
<s> <loc loc="source.child.child(2)"/> <loc loc="source.child.child(4)"/> <loc loc="source.something.else"/> </s>In other words, it does the search/search_next thing for you and you can use plain iteration to step through the results. Sep 19, 2003 Finally got to try it out: it works a lot better if you append the hits to the result structure.
XMLAPI XML * xml_search_all (XML * start, const char * element, const char * attr, const char * value)
{
XML * result = xml_create ("s");
XML * hit;
int count = 0;
XML * cur;
cur = xml_search (start, element, attr, value);
while (cur) {
hit = xml_create ("loc");
xml_set_nodup (hit, "loc", xml_getlocbuf (cur));
xml_append (result, hit);
count++;
cur = xml_search_next (start, cur, element, attr, value);
}
xml_setnum (result, "count", count);
return result;
}
XMLAPI XML * xml_searchf_all (XML * start, const char * element, const char * attr, const char * format, ...);
|
| This code and documentation are released under the terms of the GNU license. They are copyright (c) 2000-2003, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license. This presentation was created using LPML. |