Re: [xslt] Extracting just the value of a property



On Tue, 2008-02-26 at 21:39 -0500, Syd Bauman wrote:

> 
> I'd love to see this, too, Liam.

It is, so to speak, my dirty underwear :) that is, I never wrote it
for redistribution...

I think the code can be compiled either for XPath or for XPointer.

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org www.advogato.org
> 
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpointer.h>

#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>

static xmlXPathObjectPtr matchXPathExpression(
	char *filename, xmlDocPtr doc, xmlChar *expression
);

static xmlXPathObjectPtr matchXPointerExpression(
    char *filename, xmlDocPtr doc, xmlChar *expression
);

static char *commandname = "parse";

int main(int argc, char *argv[])
{
    xmlDocPtr myTree;
    char *filename;
    int xpointer = 0; /* 1 to use xpointer rather than path */
    int i;

    commandname = argv[0];

    if (argc >= 4) {
	if (!strcmp(argv[1], "-xpointer")) {
	    xpointer = 1;
	    --argc;
	    ++argv;
	}
    }

    if (argc < 3) {
	fprintf(stderr, "usage: %s [-xpointer] filename xpath-expr [...]\n", commandname);
	exit(1);
    }

    filename = argv[1];

    myTree = xmlParseFile(filename);

    if (!myTree) {
	fprintf(stderr, "%s: %s: the parse failed. Alas, I must leave you.\n",
	    commandname, filename
	);
	exit(1);
    }

    for (i = 2; i < argc; i++) {
	/* now extract the given xpath expression */
	void *result = (xpointer) ?
	    matchXPointerExpression(filename, myTree, (xmlChar *) argv[i]) :
	    matchXPathExpression(filename, myTree, (xmlChar *) argv[i])
	;

	if (!result) {
	    fprintf(stderr, "%s: %s: no match for %s\n",
		commandname, filename, argv[i]
	    );
	    exit(1);
	}
    }
    exit(0);
}

static xmlXPathObjectPtr matchXPathExpression(
    char *filename, xmlDocPtr doc, xmlChar *expression
)
{
    xmlXPathContextPtr context;
    xmlXPathObjectPtr result;
    xmlChar *resultString;
    xmlXPathInit();

    context = xmlXPathNewContext(doc);

    if (!context) {
	return NULL;
    }

    result = xmlXPathEval(expression, context);
    if (!result) {
	return NULL;
    }

    resultString = xmlXPathCastToString(result);
    if (resultString) {
	printf("%s\n", resultString); /* utf8 */
    } else {
	fprintf(stderr, "%s: %s: could not convert result of XPath expression %s to string", commandname, filename, expression);
    }
    return result;
}

static xmlXPathObjectPtr matchXPointerExpression(
    char *filename, xmlDocPtr doc, xmlChar *expression
)
{
    xmlXPathContextPtr context;
    xmlXPathObjectPtr result;
    xmlChar *resultString;

    context = xmlXPtrNewContext(doc,
	    NULL, NULL /* nodeptr, origin */
    );
    if (!context) {
	return NULL;
    }

    result = xmlXPtrEval(expression, context);
    if (!result) {
	return NULL;
    }

    /* convert to string for printing */
    resultString = xmlXPathCastToString(result);
    if (resultString) {
	printf("%s\n", resultString); /* utf8 */
    } else {
	fprintf(stderr, "%s: %s: could not convert result of XPath expression %s to string", commandname, filename, expression);
    }
    return result;
}
parse: parse.c
	$(CC) $(CFLAGS) -o parse -I/usr/include/libxml2 parse.c -lxml2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]