From e1df743329bdfd94fbfdea18303c5c6c6fe13403 Mon Sep 17 00:00:00 2001 From: methuselah-0 Date: Thu, 1 Apr 2021 08:33:56 +0200 Subject: [PATCH] Add option --xpath0 --- doc/xmllint.xml | 16 ++++++++++++++++ xmllint.c | 32 +++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/doc/xmllint.xml b/doc/xmllint.xml index 1008179b..fcdc237e 100644 --- a/doc/xmllint.xml +++ b/doc/xmllint.xml @@ -70,6 +70,7 @@ + @@ -537,6 +538,21 @@ node set is serialized in full in the output. In case of an empty node set the "XPath set is empty" result will be shown and an error exit code will be returned. + Results are separated by the newline character. + + + + + + + + + Run an XPath expression given as argument and print the + result. In case of a nodeset result, each node in the + node set is serialized in full in the output. In case + of an empty node set the "XPath set is empty" result + will be shown and an error exit code will be returned. + Results are separated by the null character. diff --git a/xmllint.c b/xmllint.c index 6ca1bf54..a60e1f4f 100644 --- a/xmllint.c +++ b/xmllint.c @@ -194,6 +194,7 @@ static int sax1 = 0; #endif /* LIBXML_SAX1_ENABLED */ #ifdef LIBXML_XPATH_ENABLED static const char *xpathquery = NULL; +static const char *xpathsep = "\n"; #endif static int options = XML_PARSE_COMPACT | XML_PARSE_BIG_LINES; static int sax = 0; @@ -2095,7 +2096,7 @@ static void doXPathDump(xmlXPathObjectPtr cur) { for (i = 0;i < cur->nodesetval->nodeNr;i++) { node = cur->nodesetval->nodeTab[i]; xmlNodeDumpOutput(buf, NULL, node, 0, 0, NULL); - xmlOutputBufferWrite(buf, 1, "\n"); + xmlOutputBufferWrite(buf, 1, xpathsep); } xmlOutputBufferClose(buf); #else @@ -2104,27 +2105,27 @@ static void doXPathDump(xmlXPathObjectPtr cur) { break; } case XPATH_BOOLEAN: - if (cur->boolval) printf("true\n"); - else printf("false\n"); + if (cur->boolval) printf("true%s", xpathsep); + else printf("false%s", xpathsep); break; case XPATH_NUMBER: switch (xmlXPathIsInf(cur->floatval)) { case 1: - printf("Infinity\n"); + printf("Infinity%s", xpathsep); break; case -1: - printf("-Infinity\n"); + printf("-Infinity%s", xpathsep); break; default: if (xmlXPathIsNaN(cur->floatval)) { - printf("NaN\n"); + printf("NaN%s", xpathsep); } else { - printf("%0g\n", cur->floatval); + printf("%0g%s", cur->floatval, xpathsep); } } break; case XPATH_STRING: - printf("%s\n", (const char *) cur->stringval); + printf("%s%s", (const char *) cur->stringval, xpathsep); break; case XPATH_UNDEFINED: fprintf(stderr, "XPath Object is uninitialized\n"); @@ -3098,7 +3099,8 @@ static void usage(FILE *f, const char *name) { fprintf(f, "\t--sax: do not build a tree but work just at the SAX level\n"); fprintf(f, "\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n"); #ifdef LIBXML_XPATH_ENABLED - fprintf(f, "\t--xpath expr: evaluate the XPath expression, imply --noout\n"); + fprintf(f, "\t--xpath expr: evaluate the XPath expression, results are separated by \\n, imply --noout\n"); + fprintf(f, "\t--xpath0 expr: evaluate the XPath expression, results are separated by \\0, imply --noout\n"); #endif fprintf(f, "\nLibxml project home page: http://xmlsoft.org/\n"); @@ -3480,6 +3482,13 @@ main(int argc, char **argv) { i++; noout++; xpathquery = argv[i]; + xpathsep = "\n"; + } else if ((!strcmp(argv[i], "-xpath0")) || + (!strcmp(argv[i], "--xpath0"))) { + i++; + noout++; + xpathquery = argv[i]; + xpathsep = "\0"; #endif } else if ((!strcmp(argv[i], "-oldxml10")) || (!strcmp(argv[i], "--oldxml10"))) { @@ -3712,6 +3721,11 @@ main(int argc, char **argv) { i++; continue; } + if ((!strcmp(argv[i], "-xpath0")) || + (!strcmp(argv[i], "--xpath0"))) { + i++; + continue; + } #endif if ((timing) && (repeat)) startTimer(); -- 2.30.2