2 * testXPath.c : a small tester program for XPath.
4 * See Copyright for the status of this software.
10 #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_DEBUG_ENABLED)
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
17 #ifdef HAVE_SYS_STAT_H
31 #include <libxml/xpath.h>
32 #include <libxml/tree.h>
33 #include <libxml/parser.h>
34 #include <libxml/debugXML.h>
35 #include <libxml/xmlmemory.h>
36 #include <libxml/parserInternals.h>
37 #include <libxml/xpathInternals.h>
38 #include <libxml/xmlerror.h>
39 #include <libxml/globals.h>
40 #if defined(LIBXML_XPTR_ENABLED)
41 #include <libxml/xpointer.h>
48 static int nocdata
= 0;
49 static xmlDocPtr document
= NULL
;
54 static xmlChar buffer
[] =
55 "<?xml version=\"1.0\"?>\n\
56 <EXAMPLE prop1=\"gnome is great\" prop2=\"& linux too\">\n\
58 <title>Welcome to Gnome</title>\n\
61 <title>The Linux adventure</title>\n\
62 <p>bla bla bla ...</p>\n\
63 <image href=\"linus.gif\"/>\n\
67 <title>Chapter 2</title>\n\
68 <p>this is chapter 2 ...</p>\n\
71 <title>Chapter 3</title>\n\
72 <p>this is chapter 3 ...</p>\n\
79 testXPath(const char *str
) {
80 xmlXPathObjectPtr res
;
81 xmlXPathContextPtr ctxt
;
83 #if defined(LIBXML_XPTR_ENABLED)
85 ctxt
= xmlXPtrNewContext(document
, NULL
, NULL
);
86 res
= xmlXPtrEval(BAD_CAST str
, ctxt
);
89 ctxt
= xmlXPathNewContext(document
);
90 ctxt
->node
= xmlDocGetRootElement(document
);
92 res
= xmlXPathEvalExpression(BAD_CAST str
, ctxt
);
94 /* res = xmlXPathEval(BAD_CAST str, ctxt); */
95 xmlXPathCompExprPtr comp
;
97 comp
= xmlXPathCompile(BAD_CAST str
);
100 xmlXPathDebugDumpCompExpr(stdout
, comp
, 0);
102 res
= xmlXPathCompiledEval(comp
, ctxt
);
103 xmlXPathFreeCompExpr(comp
);
107 #if defined(LIBXML_XPTR_ENABLED)
110 xmlXPathDebugDumpObject(stdout
, res
, 0);
111 xmlXPathFreeObject(res
);
112 xmlXPathFreeContext(ctxt
);
116 testXPathFile(const char *filename
) {
118 char expression
[5000];
121 input
= fopen(filename
, "r");
123 xmlGenericError(xmlGenericErrorContext
,
124 "Cannot open %s for reading\n", filename
);
127 while (fgets(expression
, 4500, input
) != NULL
) {
128 len
= strlen(expression
);
131 ((expression
[len
] == '\n') || (expression
[len
] == '\t') ||
132 (expression
[len
] == '\r') || (expression
[len
] == ' '))) len
--;
133 expression
[len
+ 1] = 0;
135 printf("\n========================\nExpression: %s\n", expression
) ;
136 testXPath(expression
);
143 int main(int argc
, char **argv
) {
147 char *filename
= NULL
;
149 for (i
= 1; i
< argc
; i
++) {
150 #if defined(LIBXML_XPTR_ENABLED)
151 if ((!strcmp(argv
[i
], "-xptr")) || (!strcmp(argv
[i
], "--xptr")))
155 if ((!strcmp(argv
[i
], "-debug")) || (!strcmp(argv
[i
], "--debug")))
157 else if ((!strcmp(argv
[i
], "-valid")) || (!strcmp(argv
[i
], "--valid")))
159 else if ((!strcmp(argv
[i
], "-expr")) || (!strcmp(argv
[i
], "--expr")))
161 else if ((!strcmp(argv
[i
], "-tree")) || (!strcmp(argv
[i
], "--tree")))
163 else if ((!strcmp(argv
[i
], "-nocdata")) ||
164 (!strcmp(argv
[i
], "--nocdata")))
166 else if ((!strcmp(argv
[i
], "-i")) || (!strcmp(argv
[i
], "--input")))
167 filename
= argv
[++i
];
168 else if ((!strcmp(argv
[i
], "-f")) || (!strcmp(argv
[i
], "--file")))
171 if (valid
!= 0) xmlDoValidityCheckingDefaultValue
= 1;
172 xmlLoadExtDtdDefaultValue
|= XML_DETECT_IDS
;
173 xmlLoadExtDtdDefaultValue
|= XML_COMPLETE_ATTRS
;
174 xmlSubstituteEntitiesDefaultValue
= 1;
176 xmlDefaultSAXHandlerInit();
177 xmlDefaultSAXHandler
.cdataBlock
= NULL
;
179 if (document
== NULL
) {
180 if (filename
== NULL
)
181 document
= xmlReadDoc(buffer
,NULL
,NULL
,XML_PARSE_COMPACT
);
183 document
= xmlReadFile(filename
,NULL
,XML_PARSE_COMPACT
);
185 for (i
= 1; i
< argc
; i
++) {
186 if ((!strcmp(argv
[i
], "-i")) || (!strcmp(argv
[i
], "--input"))) {
189 if (argv
[i
][0] != '-') {
191 testXPathFile(argv
[i
]);
198 printf("Usage : %s [--debug] [--copy] stringsorfiles ...\n",
200 printf("\tParse the XPath strings and output the result of the parsing\n");
201 printf("\t--debug : dump a debug version of the result\n");
202 printf("\t--valid : switch on DTD support in the parser\n");
203 #if defined(LIBXML_XPTR_ENABLED)
204 printf("\t--xptr : expressions are XPointer expressions\n");
206 printf("\t--expr : debug XPath expressions only\n");
207 printf("\t--tree : show the compiled XPath tree\n");
208 printf("\t--nocdata : do not generate CDATA nodes\n");
209 printf("\t--input filename : or\n");
210 printf("\t-i filename : read the document from filename\n");
211 printf("\t--file : or\n");
212 printf("\t-f : read queries from files, args\n");
214 if (document
!= NULL
)
215 xmlFreeDoc(document
);
223 int main(int argc ATTRIBUTE_UNUSED
, char **argv ATTRIBUTE_UNUSED
) {
224 printf("%s : XPath/Debug support not compiled in\n", argv
[0]);
227 #endif /* LIBXML_XPATH_ENABLED */