1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
7 * \brief XML node inspection utilities
17 int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem)
19 if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
28 int yaz_match_xsd_string_n_nmem(xmlNodePtr ptr, const char *elem, NMEM nmem,
32 struct _xmlAttr *attr;
34 if (!yaz_match_xsd_element(ptr, elem))
37 for (attr = ptr->properties; attr; attr = attr->next)
38 if (!strcmp(attr->name, "type") &&
39 attr->children && attr->children->type == XML_TEXT_NODE)
41 const char *t = strchr(attr->children->content, ':');
45 t = attr->children->content;
46 if (!strcmp(t, "string"))
53 if (!ptr || ptr->type != XML_TEXT_NODE)
58 *val = nmem_strdup(nmem, (const char *) ptr->content);
60 *len = xmlStrlen(ptr->content);
64 int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
67 return yaz_match_xsd_string_n_nmem(ptr, elem, o->mem, val, len);
70 int yaz_match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val)
72 return yaz_match_xsd_string_n(ptr, elem, o, val, 0);
75 int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o,
76 char **val, int *len, int fixup_root)
79 int no_root_nodes = 0;
81 if (!yaz_match_xsd_element(ptr, elem))
84 buf = xmlBufferCreate();
86 /* Copy each element nodes at top.
87 In most cases there is only one root node.. At least one server
88 http://www.theeuropeanlibrary.org/sru/sru.pl
89 has multiple root nodes in recordData.
91 for (ptr = ptr->children; ptr; ptr = ptr->next)
93 if (ptr->type == XML_ELEMENT_NODE)
95 /* copy node to get NS right (bug #740). */
96 xmlNode *tmp = xmlCopyNode(ptr, 1);
98 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
104 if (no_root_nodes != 1 && fixup_root)
106 /* does not appear to be an XML document. Make it so */
107 xmlBufferAddHead(buf, (const xmlChar *) "<yaz_record>", -1);
108 xmlBufferAdd(buf, (const xmlChar *) "</yaz_record>", -1);
110 *val = odr_strdupn(o, (const char *) buf->content, buf->use);
119 int yaz_match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
120 char **val, int *len)
122 return yaz_match_xsd_XML_n2(ptr, elem, o, val, len, 0);
125 int yaz_match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
129 struct _xmlAttr *attr;
131 if (!yaz_match_xsd_element(ptr, elem))
134 for (attr = ptr->properties; attr; attr = attr->next)
135 if (!strcmp(attr->name, "type") &&
136 attr->children && attr->children->type == XML_TEXT_NODE)
138 const char *t = strchr(attr->children->content, ':');
142 t = attr->children->content;
143 if (!strcmp(t, "integer"))
150 if (!ptr || ptr->type != XML_TEXT_NODE)
152 *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
163 * c-file-style: "Stroustrup"
164 * indent-tabs-mode: nil
166 * vim: shiftwidth=4 tabstop=8 expandtab