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 Implements SRU 2.0 facets
16 #include <yaz/wrbuf.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
24 #include <yaz/pquery.h>
25 #include <yaz/facet.h>
27 void yaz_sru_facet_request(ODR o, Z_FacetList **facetList, const char **limit)
29 if (o->direction == ODR_ENCODE)
31 Z_FacetList *fl = *facetList;
35 WRBUF w = wrbuf_alloc();
36 for (i = 0; i < fl->num; i++)
38 struct yaz_facet_attr av;
39 yaz_facet_attr_init(&av);
40 yaz_facet_attr_get_z_attributes(fl->elements[i]->attributes,
44 wrbuf_printf(w, "%d", av.limit ? av.limit : -1);
46 wrbuf_printf(w, ":%s,", av.useattr);
47 /* av.relation not considered yet */
52 wrbuf_cut_right(w, 1); /* remove , */
53 *limit = odr_strdup(o, wrbuf_cstr(w));
58 else if (o->direction == ODR_DECODE)
60 const char *cp = *limit;
66 WRBUF w = wrbuf_alloc();
67 while (sscanf(cp, "%d%n", &limit_val, &nor) >= 1 && nor > 0)
72 if (*cp == ':') /* field name follows */
74 wrbuf_puts(w, "@attr 1=");
75 while (*++cp && *cp != ',')
80 wrbuf_printf(w, "@attr 3=%d", limit_val);
86 *facetList = yaz_pqf_parse_facet_list(o, wrbuf_cstr(w));
93 void yaz_sru_facet_response(ODR o, Z_FacetList **facetList, xmlNodePtr n)
95 if (o->direction == ODR_ENCODE)
97 Z_FacetList *fl = *facetList;
102 "http://docs.oasis-open.org/ns/search-ws/facetedResults";
103 xmlNode *p1 = xmlNewChild(n, 0, BAD_CAST "facetedResults", 0);
104 xmlNsPtr ns_fr = xmlNewNs(p1, BAD_CAST ns, BAD_CAST "fr");
106 for (i = 0; i < fl->num; i++)
108 Z_FacetField *ff = fl->elements[i];
109 xmlNode *p2 = xmlNewChild(p1, 0, BAD_CAST "facet", 0);
112 struct yaz_facet_attr av;
113 yaz_facet_attr_init(&av);
114 yaz_facet_attr_get_z_attributes(ff->attributes, &av);
115 add_xsd_string(p2, "index", av.useattr);
116 p3 = xmlNewChild(p2, 0, BAD_CAST "terms", 0);
117 for (j = 0; j < ff->num_terms; j++)
119 Z_FacetTerm *ft = ff->terms[j];
120 Z_Term *zt = ft->term;
121 xmlNode *p4 = xmlNewChild(p3, 0, BAD_CAST "term", 0);
122 if (zt->which == Z_Term_general)
123 add_xsd_string_n(p4, "actualTerm",
124 (char *) zt->u.general->buf,
127 add_xsd_integer(p4, "count", ft->count);
140 * c-file-style: "Stroustrup"
141 * indent-tabs-mode: nil
143 * vim: shiftwidth=4 tabstop=8 expandtab