mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-10-27 12:15:34 +03:00
Add newlines to 'xmllint --xpath' output
Separate nodes in a node-set with newlines and always add a terminating newline. This is a breaking change but the old behavior of dumping text nodes without separator was mostly useless. Also use buffered I/O when dumping node-sets.
This commit is contained in:
25
xmllint.c
25
xmllint.c
@@ -2071,51 +2071,52 @@ static void doXPathDump(xmlXPathObjectPtr cur) {
|
|||||||
int i;
|
int i;
|
||||||
xmlNodePtr node;
|
xmlNodePtr node;
|
||||||
#ifdef LIBXML_OUTPUT_ENABLED
|
#ifdef LIBXML_OUTPUT_ENABLED
|
||||||
xmlSaveCtxtPtr ctxt;
|
xmlOutputBufferPtr buf;
|
||||||
|
|
||||||
if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr <= 0)) {
|
if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr <= 0)) {
|
||||||
fprintf(stderr, "XPath set is empty\n");
|
fprintf(stderr, "XPath set is empty\n");
|
||||||
progresult = XMLLINT_ERR_XPATH;
|
progresult = XMLLINT_ERR_XPATH;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ctxt = xmlSaveToFd(1, NULL, 0);
|
buf = xmlOutputBufferCreateFile(stdout, NULL);
|
||||||
if (ctxt == NULL) {
|
if (buf == NULL) {
|
||||||
fprintf(stderr, "Out of memory for XPath\n");
|
fprintf(stderr, "Out of memory for XPath\n");
|
||||||
progresult = XMLLINT_ERR_MEM;
|
progresult = XMLLINT_ERR_MEM;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (i = 0;i < cur->nodesetval->nodeNr;i++) {
|
for (i = 0;i < cur->nodesetval->nodeNr;i++) {
|
||||||
node = cur->nodesetval->nodeTab[i];
|
node = cur->nodesetval->nodeTab[i];
|
||||||
xmlSaveTree(ctxt, node);
|
xmlNodeDumpOutput(buf, node->doc, node, 0, 0, NULL);
|
||||||
|
xmlOutputBufferWrite(buf, 1, "\n");
|
||||||
}
|
}
|
||||||
xmlSaveClose(ctxt);
|
xmlOutputBufferClose(buf);
|
||||||
#else
|
#else
|
||||||
printf("xpath returned %d nodes\n", cur->nodesetval->nodeNr);
|
printf("xpath returned %d nodes\n", cur->nodesetval->nodeNr);
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case XPATH_BOOLEAN:
|
case XPATH_BOOLEAN:
|
||||||
if (cur->boolval) printf("true");
|
if (cur->boolval) printf("true\n");
|
||||||
else printf("false");
|
else printf("false\n");
|
||||||
break;
|
break;
|
||||||
case XPATH_NUMBER:
|
case XPATH_NUMBER:
|
||||||
switch (xmlXPathIsInf(cur->floatval)) {
|
switch (xmlXPathIsInf(cur->floatval)) {
|
||||||
case 1:
|
case 1:
|
||||||
printf("Infinity");
|
printf("Infinity\n");
|
||||||
break;
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
printf("-Infinity");
|
printf("-Infinity\n");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (xmlXPathIsNaN(cur->floatval)) {
|
if (xmlXPathIsNaN(cur->floatval)) {
|
||||||
printf("NaN");
|
printf("NaN\n");
|
||||||
} else {
|
} else {
|
||||||
printf("%0g", cur->floatval);
|
printf("%0g\n", cur->floatval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case XPATH_STRING:
|
case XPATH_STRING:
|
||||||
printf("%s", (const char *) cur->stringval);
|
printf("%s\n", (const char *) cur->stringval);
|
||||||
break;
|
break;
|
||||||
case XPATH_UNDEFINED:
|
case XPATH_UNDEFINED:
|
||||||
fprintf(stderr, "XPath Object is uninitialized\n");
|
fprintf(stderr, "XPath Object is uninitialized\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user