From c43b7da65788fb728cfb36d1f5909e0a4f5bcb11 Mon Sep 17 00:00:00 2001 From: MDT 2001 John Fleck Date: Mon, 23 Jul 2001 15:34:28 +0000 Subject: [PATCH] updating libxslt tutorial to include param support Mon Jul 23 09:32:27 MDT 2001 John Fleck * updating libxslt tutorial to include param support --- ChangeLog | 4 + doc/tutorial/libxslt_tutorial.c | 34 ++++- doc/tutorial/libxslttutorial.html | 217 +++++++++++++++++++----------- doc/tutorial/libxslttutorial.xml | 49 +++++-- 4 files changed, 212 insertions(+), 92 deletions(-) diff --git a/ChangeLog b/ChangeLog index b94610a7..3aae2b96 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Mon Jul 23 09:32:27 MDT 2001 John Fleck + + * updating libxslt tutorial to include param support + Mon Jul 23 20:12:38 CEST 2001 Daniel Veillard * libexslt/math.c: small cleanup diff --git a/doc/tutorial/libxslt_tutorial.c b/doc/tutorial/libxslt_tutorial.c index 2ab1d8a8..02770efa 100644 --- a/doc/tutorial/libxslt_tutorial.c +++ b/doc/tutorial/libxslt_tutorial.c @@ -21,6 +21,7 @@ * */ +#include #include #include #include @@ -39,10 +40,15 @@ extern int xmlLoadExtDtdDefaultValue; static void usage(const char *name) { printf("Usage: %s [options] stylesheet file [file ...]\n", name); + printf(" --param name value : pass a (parameter,value) pair\n"); + } int main(int argc, char **argv) { + int i; + const char *params[16 + 1]; + int nbparams = 0; xsltStylesheetPtr cur = NULL; xmlDocPtr doc, res; @@ -50,12 +56,34 @@ main(int argc, char **argv) { usage(argv[0]); return(1); } + + for (i = 1; i < argc; i++) { + if (argv[i][0] != '-') + break; + if ((!strcmp(argv[i], "-param")) || + (!strcmp(argv[i], "--param"))) { + i++; + params[nbparams++] = argv[i++]; + params[nbparams++] = argv[i]; + if (nbparams >= 16) { + fprintf(stderr, "too many params\n"); + return (1); + } + } else { + fprintf(stderr, "Unknown option %s\n", argv[i]); + usage(argv[0]); + return (1); + } + } + + params[nbparams] = NULL; xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; - cur = xsltParseStylesheetFile((const xmlChar *)argv[1]); - doc = xmlParseFile(argv[2]); - res = xsltApplyStylesheet(cur, doc, NULL); + cur = xsltParseStylesheetFile((const xmlChar *)argv[i]); + i++; + doc = xmlParseFile(argv[i]); + res = xsltApplyStylesheet(cur, doc, params); xsltSaveResultToFile(stdout, res, cur); xsltFreeStylesheet(cur); diff --git a/doc/tutorial/libxslttutorial.html b/doc/tutorial/libxslttutorial.html index 1b1a5b2c..d66be9fb 100644 --- a/doc/tutorial/libxslttutorial.html +++ b/doc/tutorial/libxslttutorial.html @@ -3,18 +3,17 @@ libxslt Tutorial - + -
+

-libxslt Tutorial

+libxslt Tutorial

John Fleck

-
- This is version 0.2 of the libxslt Tutorial -
-
-
+

+ This is version 0.3 of the libxslt Tutorial +

+

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version @@ -41,15 +40,18 @@

Saving the result
+
Parameters +
Cleanup
+
A The Code +

- -Abstract +Abstract

A tutorial on building a simple application using the libxslt library to perform @@ -57,11 +59,8 @@ XML file into HTML.

- -

- -Introduction -

+

+Introduction

The Extensible Markup Language (XML) is a World Wide Web Consortium standard for the exchange of structured data in text form. Its popularity stems from its universality. Any computer can @@ -81,7 +80,8 @@ transformations.

-

Note

+

+Note

While libxslt was written under the auspices of the GNOME project, it does not @@ -108,45 +108,55 @@

References:

- -

- -Primary Functions -

+

+Primary Functions

+

To transform an XML file, you must perform three functions:

  1. -parse the input file

  2. +parse the input file

  3. -parse the stylesheet

  4. +parse the stylesheet

  5. -apply the stylesheet

  6. +apply the stylesheet

- -

- -Preparing to Parse -

+

+Preparing to Parse

Before you can begin parsing input files or stylesheets, there are several steps you need to take to set up entity handling. These steps are not unique to libxslt. Any @@ -162,20 +172,17 @@

Second, set xmlLoadExtDtdDefaultValue equal to 1. This tells libxml - to load external entity subsets. If you do not do this and the file your + to load external entity subsets. If you do not do this and your input file includes entities through external subsets, you will get errors.

- -

- -Parse the Stylesheet -

+

+Parse the Stylesheet

Parsing the stylesheet takes a single function call, which takes a variable of type xmlChar:

-	  cur = xsltParseStylesheetFile((const xmlChar *)argv[1]);
+	  cur = xsltParseStylesheetFile((const xmlChar *)argv[i]);
 	
In this case, I cast the stylesheet file name, passed in as a command line argument, to xmlChar. The return value @@ -186,14 +193,11 @@

- -

- -Parse the Input File -

+

+Parse the Input File

Parsing the input file takes a single function call:

-doc = xmlParseFile(argv[2]);
+doc = xmlParseFile(argv[i]);
 	
It returns an xmlDocPtr, a struct in memory that contains the document tree. It can be manipulated directly, but for this @@ -201,30 +205,24 @@ doc = xmlParseFile(argv[2]);

- -

- -Applying the Stylesheet -

+

+Applying the Stylesheet

Now that you have trees representing the document and the stylesheet in memory, apply the stylesheet to the document. The function that does this is xsltApplyStylesheet:

-res = xsltApplyStylesheet(cur, doc, NULL);
+res = xsltApplyStylesheet(cur, doc, params);
 	
- For parameters, the function takes an xsltStylesheetPtr and an + The function takes an xsltStylesheetPtr and an xmlDocPtr, the values returned by the previous two functions. The third - parameter, NULL in this case, can be used to pass parameters to the - stylesheet. It is a NULL-terminated array of name/value pairs of const - char's. + variable, params can be used to pass + XSLT parameters to the stylesheet. It is a + NULL-terminated array of name/value pairs of const char's.

- -

- -Saving the result -

+

+Saving the result

libxslt includes a family of functions to use in saving the resulting output. For this example, xsltSaveResultToFile is used, and the results are @@ -235,7 +233,8 @@ xsltSaveResultToFile(stdout, res, cur);

-

Note

+

+Note

libxml also contains output functions, such as xmlSaveFile, which can be used here. However, output-related information contained in the @@ -246,11 +245,39 @@ xsltSaveResultToFile(stdout, res, cur);

- -

- -Cleanup -

+

+Parameters

+

+ In XSLT, parameters may be used as a way to pass + additional information to a + stylesheet. libxslt accepts + XSLT parameters as one of the values passed to + xsltApplyStylesheet. +

+

+ In the tutorial example and in xsltproc, + on which the tutorial example is based, parameters to be passed take the + form of key-value pairs. The program collects them from command line + arguments, inserting them in the array params, then + passes them to the function. The final element in the array is set to + NULL. + +

+

+Note

+

+ If a parameter being passed is a string rather than an + XSLT node, it must be escaped. For the tutorial + program, that would be done as follows: + tutorial]$ ./libxslt_tutorial --param rootid "'asect1'" + stylesheet.xsl filename.xml +

+
+

+
+
+

+Cleanup

After you are finished, libxslt and libxml provide functions for deallocating memory. @@ -263,29 +290,31 @@ xsltSaveResultToFile(stdout, res, cur); xmlFreeDoc(doc);3 -

+
+
+1 +2 +3 -
-1

Free the memory used by your stylesheet.

-2

Free the memory used by the results document.

-3

Free the memory used by your original document.

+ +

-
-

-Appendix A. The Code

+
+

+A. The Code

libxslt_tutorial.c

@@ -311,6 +340,7 @@ xsltSaveResultToFile(stdout, res, cur);
  *
  */ 
 
+#include <string.h>
 #include <libxml/xmlmemory.h>
 #include <libxml/debugXML.h>
 #include <libxml/HTMLtree.h>
@@ -329,10 +359,15 @@ extern int xmlLoadExtDtdDefaultValue;
 
 static void usage(const char *name) {
     printf("Usage: %s [options] stylesheet file [file ...]\n", name);
+    printf("      --param name value : pass a (parameter,value) pair\n");
+
 }
 
 int
 main(int argc, char **argv) {
+	int i;
+	const char *params[16 + 1];
+	int nbparams = 0;
 	xsltStylesheetPtr cur = NULL;
 	xmlDocPtr doc, res;
 
@@ -340,12 +375,34 @@ main(int argc, char **argv) {
 		usage(argv[0]);
 		return(1);
 	}
+	
 
+ for (i = 1; i < argc; i++) {
+        if (argv[i][0] != '-')
+            break;
+	if ((!strcmp(argv[i], "-param")) ||
+                   (!strcmp(argv[i], "--param"))) {
+		i++;
+		params[nbparams++] = argv[i++];
+		params[nbparams++] = argv[i];
+		if (nbparams >= 16) {
+			fprintf(stderr, "too many params\n");
+			return (1);
+		}
+        }  else {
+            fprintf(stderr, "Unknown option %s\n", argv[i]);
+            usage(argv[0]);
+            return (1);
+        }
+    }
+
+	params[nbparams] = NULL;
 	xmlSubstituteEntitiesDefault(1);
 	xmlLoadExtDtdDefaultValue = 1;
-	cur = xsltParseStylesheetFile((const xmlChar *)argv[1]);
-	doc = xmlParseFile(argv[2]);
-	res = xsltApplyStylesheet(cur, doc, NULL);
+	cur = xsltParseStylesheetFile((const xmlChar *)argv[i]);
+	i++;
+	doc = xmlParseFile(argv[i]);
+	res = xsltApplyStylesheet(cur, doc, params);
 	xsltSaveResultToFile(stdout, res, cur);
 
 	xsltFreeStylesheet(cur);
diff --git a/doc/tutorial/libxslttutorial.xml b/doc/tutorial/libxslttutorial.xml
index 981bbfc0..1e299ff2 100644
--- a/doc/tutorial/libxslttutorial.xml
+++ b/doc/tutorial/libxslttutorial.xml
@@ -26,7 +26,7 @@
       Fleck
     
     
-      This is version 0.2 of the libxslt Tutorial
+      This is version 0.3 of the libxslt Tutorial
     
   
   
@@ -136,7 +136,7 @@
 
       Second, set xmlLoadExtDtdDefaultValue equal to
 	1. This tells libxml
-	to load external entity subsets. If you do not do this and the file your
+	to load external entity subsets. If you do not do this and your
 	input file includes entities through external subsets, you will get
 	errors.
     
@@ -145,7 +145,7 @@
       Parsing the stylesheet takes a single function call, which takes a
 	variable of type xmlChar:
 	
-	  cur = xsltParseStylesheetFile((const xmlChar *)argv[1]);
+	  cur = xsltParseStylesheetFile((const xmlChar *)argv[i]);
 	
 	In this case, I cast the stylesheet file name, passed in as a
 	command line argument, to xmlChar. The return value
@@ -160,7 +160,7 @@
       Parse the Input File
       Parsing the input file takes a single function call:
 	
-doc = xmlParseFile(argv[2]);
+doc = xmlParseFile(argv[i]);
 	
 	It returns an xmlDocPtr, a struct in memory that
 	contains the document tree. It can be manipulated directly, but for this
@@ -174,13 +174,13 @@ doc = xmlParseFile(argv[2]);
 	in memory, apply the stylesheet to the document. The
 	function that does this is xsltApplyStylesheet:
 	
-res = xsltApplyStylesheet(cur, doc, NULL);
+res = xsltApplyStylesheet(cur, doc, params);
 	
-	For parameters, the function takes an xsltStylesheetPtr and an
+	The function takes an xsltStylesheetPtr and an
 	xmlDocPtr, the values returned by the previous two functions. The third
-	parameter, NULL in this case, can be used to pass parameters to the
-	stylesheet. It is a NULL-terminated array of name/value pairs of const
-	char's.
+	variable, params can be used to pass
+	XSLT parameters to the stylesheet. It is a
+	NULL-terminated array of name/value pairs of const char's.
       
     
 
@@ -206,6 +206,37 @@ xsltSaveResultToFile(stdout, res, cur);
       
     
 
+    
+      Parameters
+      
+	In XSLT, parameters may be used as a way to pass
+	additional information to a
+	stylesheet. libxslt accepts
+	XSLT parameters as one of the values passed to
+	xsltApplyStylesheet.
+      
+      
+      
+	In the tutorial example and in xsltproc,
+	on which the tutorial example is based, parameters to be passed take the
+	form of key-value pairs. The program collects them from command line
+	arguments, inserting them in the array params, then
+	passes them to the function. The final element in the array is set to
+	NULL.
+
+	
+	  
+	    If a parameter being passed is a string rather than an
+	    XSLT node, it must be escaped. For the tutorial
+	    program, that would be done as follows:
+	    tutorial]$ ./libxslt_tutorial --param rootid "'asect1'"
+	    stylesheet.xsl filename.xml
+	  
+	
+      
+
+    
+
     
       Cleanup
       After you are finished, libxslt and