1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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(xmlNodePtr ptr, const char *elem, ODR o,
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 = odr_strdup(o, (const char *) ptr->content);
60 *len = xmlStrlen(ptr->content);
65 int yaz_match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val)
67 return yaz_match_xsd_string_n(ptr, elem, o, val, 0);
70 int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o,
71 char **val, int *len, int fixup_root)
74 int no_root_nodes = 0;
76 if (!yaz_match_xsd_element(ptr, elem))
79 buf = xmlBufferCreate();
81 /* Copy each element nodes at top.
82 In most cases there is only one root node.. At least one server
83 http://www.theeuropeanlibrary.org/sru/sru.pl
84 has multiple root nodes in recordData.
86 for (ptr = ptr->children; ptr; ptr = ptr->next)
88 if (ptr->type == XML_ELEMENT_NODE)
90 /* copy node to get NS right (bug #740). */
91 xmlNode *tmp = xmlCopyNode(ptr, 1);
93 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
99 if (no_root_nodes != 1 && fixup_root)
101 /* does not appear to be an XML document. Make it so */
102 xmlBufferAddHead(buf, (const xmlChar *) "<yaz_record>", -1);
103 xmlBufferAdd(buf, (const xmlChar *) "</yaz_record>", -1);
105 *val = (char *) odr_malloc(o, buf->use + 1);
106 memcpy(*val, buf->content, buf->use);
107 (*val)[buf->use] = '\0';
117 int yaz_match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
118 char **val, int *len)
120 return yaz_match_xsd_XML_n2(ptr, elem, o, val, len, 0);
123 int yaz_match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
127 struct _xmlAttr *attr;
129 if (!yaz_match_xsd_element(ptr, elem))
132 for (attr = ptr->properties; attr; attr = attr->next)
133 if (!strcmp(attr->name, "type") &&
134 attr->children && attr->children->type == XML_TEXT_NODE)
136 const char *t = strchr(attr->children->content, ':');
140 t = attr->children->content;
141 if (!strcmp(t, "integer"))
148 if (!ptr || ptr->type != XML_TEXT_NODE)
150 *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
161 * c-file-style: "Stroustrup"
162 * indent-tabs-mode: nil
164 * vim: shiftwidth=4 tabstop=8 expandtab