2 * Copyright (C) 1995-2005, Index Data ApS
5 * $Id: xmlquery.c,v 1.2 2006-01-30 14:02:07 adam Exp $
10 * \brief Query / XML conversions
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
20 #include <yaz/logrpn.h>
21 #include <yaz/xmlquery.h>
23 void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
27 const char *setname = 0;
29 if (element->attributeSet)
32 attrset = oid_getentbyoid (element->attributeSet);
33 setname = attrset->desc;
36 if (element->which == Z_AttributeValue_numeric)
38 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
41 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
43 sprintf(formstr, "%d", *element->attributeType);
44 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
46 sprintf(formstr, "%d", *element->value.numeric);
47 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
49 else if (element->which == Z_AttributeValue_complex)
52 for (i = 0; i<element->value.complex->num_list; i++)
54 xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
57 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
59 sprintf(formstr, "%d", *element->attributeType);
60 xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
62 if (element->value.complex->list[i]->which ==
63 Z_StringOrNumeric_string)
65 xmlNewProp(node, BAD_CAST "value", BAD_CAST
66 element->value.complex->list[i]->u.string);
68 else if (element->value.complex->list[i]->which ==
69 Z_StringOrNumeric_numeric)
71 sprintf(formstr, "%d",
72 *element->value.complex->list[i]->u.numeric);
73 xmlNewProp(node, BAD_CAST "value", BAD_CAST formstr);
80 xmlNodePtr yaz_query2xml_term(const Z_Term *term,
84 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "term", 0);
90 t = xmlNewTextLen(BAD_CAST term->u.general->buf, term->u.general->len);
92 case Z_Term_characterString:
93 t = xmlNewText(BAD_CAST term->u.characterString);
96 sprintf(formstr, "%d", *term->u.numeric);
97 t = xmlNewText(BAD_CAST formstr);
104 if (t) /* got a term node ? */
105 xmlAddChild(node, t);
109 xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
112 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "apt", 0);
113 int num_attributes = zapt->attributes->num_attributes;
115 for (i = 0; i<num_attributes; i++)
116 yaz_query2xml_attribute_element(zapt->attributes->attributes[i], node);
117 yaz_query2xml_term(zapt->term, node);
123 void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
125 const char *type = 0;
134 case Z_Operator_and_not:
137 case Z_Operator_prox:
143 xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
145 if (op->which == Z_Operator_prox)
149 if (op->u.prox->exclusion)
151 if (*op->u.prox->exclusion)
152 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "true");
154 xmlNewProp(node, BAD_CAST "exclusion", BAD_CAST "false");
156 sprintf(formstr, "%d", *op->u.prox->distance);
157 xmlNewProp(node, BAD_CAST "distance", BAD_CAST formstr);
159 if (*op->u.prox->ordered)
160 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "true");
162 xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "false");
164 sprintf(formstr, "%d", *op->u.prox->relationType);
165 xmlNewProp(node, BAD_CAST "relationType", BAD_CAST formstr);
167 switch(op->u.prox->which)
169 case Z_ProximityOperator_known:
170 sprintf(formstr, "%d", *op->u.prox->u.known);
171 xmlNewProp(node, BAD_CAST "knownProximityUnit",
175 xmlNewProp(node, BAD_CAST "privateProximityUnit",
182 xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
185 if (zs->which == Z_RPNStructure_complex)
187 Z_Complex *zc = zs->u.complex;
189 xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "binary", 0);
191 yaz_query2xml_operator(zc->roperator, node);
192 yaz_query2xml_rpnstructure(zc->s1, node);
193 yaz_query2xml_rpnstructure(zc->s2, node);
196 else if (zs->which == Z_RPNStructure_simple)
198 if (zs->u.simple->which == Z_Operand_APT)
199 return yaz_query2xml_apt(zs->u.simple->u.attributesPlusTerm,
201 else if (zs->u.simple->which == Z_Operand_resultSetId)
202 return xmlNewChild(parent, /* NS */ 0, BAD_CAST "rset",
203 BAD_CAST zs->u.simple->u.resultSetId);
208 xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
210 oident *attrset = oid_getentbyoid (rpn->attributeSetId);
211 if (attrset && attrset->value)
212 xmlNewProp(parent, BAD_CAST "set", BAD_CAST attrset->desc);
213 return yaz_query2xml_rpnstructure(rpn->RPNStructure, parent);
216 xmlNodePtr yaz_query2xml_ccl(const Odr_oct *ccl, xmlNodePtr node)
221 xmlNodePtr yaz_query2xml_z3958(const Odr_oct *ccl, xmlNodePtr node)
226 xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
231 void yaz_rpnquery2xml(const Z_RPNQuery *rpn, void *docp_void)
235 query.which = Z_Query_type_1;
236 query.u.type_1 = (Z_RPNQuery *) rpn;
237 yaz_query2xml(&query, docp_void);
240 void yaz_query2xml(const Z_Query *q, void *docp_void)
242 xmlDocPtr *docp = (xmlDocPtr *) docp_void;
243 xmlNodePtr top_node, child_node = 0;
244 const char *type = 0;
249 top_node = xmlNewNode(0, BAD_CAST "query");
254 case Z_Query_type_101:
256 child_node = yaz_query2xml_rpn(q->u.type_1, top_node);
260 child_node = yaz_query2xml_ccl(q->u.type_2, top_node);
262 case Z_Query_type_100:
264 child_node = yaz_query2xml_z3958(q->u.type_100, top_node);
266 case Z_Query_type_104:
267 if (q->u.type_104->which == Z_External_CQL)
270 child_node = yaz_query2xml_cql(q->u.type_104->u.cql, top_node);
274 if (child_node && type)
276 *docp = xmlNewDoc(BAD_CAST "1.0");
277 xmlDocSetRootElement(*docp, top_node); /* make it top node in doc */
279 /* set type attribute now */
280 xmlNewProp(top_node, BAD_CAST "type", BAD_CAST type);
285 xmlFreeNode(top_node);
289 void yaz_xml2query(const xmlNode node, Z_Query **q, ODR odr)
302 * indent-tabs-mode: nil
304 * vim: shiftwidth=4 tabstop=8 expandtab