mirror of
https://gitlab.gnome.org/GNOME/libxslt
synced 2025-07-28 04:41:59 +03:00
updating libxslt tutorial to include param support
Mon Jul 23 09:32:27 MDT 2001 John Fleck <jfleck@inkstain.net> * updating libxslt tutorial to include param support
This commit is contained in:
committed by
John Fleck
parent
ca2e262335
commit
c43b7da657
@ -1,3 +1,7 @@
|
|||||||
|
Mon Jul 23 09:32:27 MDT 2001 John Fleck <jfleck@inkstain.net>
|
||||||
|
|
||||||
|
* updating libxslt tutorial to include param support
|
||||||
|
|
||||||
Mon Jul 23 20:12:38 CEST 2001 Daniel Veillard <daniel@veillard.com>
|
Mon Jul 23 20:12:38 CEST 2001 Daniel Veillard <daniel@veillard.com>
|
||||||
|
|
||||||
* libexslt/math.c: small cleanup
|
* libexslt/math.c: small cleanup
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
#include <libxml/xmlmemory.h>
|
#include <libxml/xmlmemory.h>
|
||||||
#include <libxml/debugXML.h>
|
#include <libxml/debugXML.h>
|
||||||
#include <libxml/HTMLtree.h>
|
#include <libxml/HTMLtree.h>
|
||||||
@ -39,10 +40,15 @@ extern int xmlLoadExtDtdDefaultValue;
|
|||||||
|
|
||||||
static void usage(const char *name) {
|
static void usage(const char *name) {
|
||||||
printf("Usage: %s [options] stylesheet file [file ...]\n", name);
|
printf("Usage: %s [options] stylesheet file [file ...]\n", name);
|
||||||
|
printf(" --param name value : pass a (parameter,value) pair\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv) {
|
main(int argc, char **argv) {
|
||||||
|
int i;
|
||||||
|
const char *params[16 + 1];
|
||||||
|
int nbparams = 0;
|
||||||
xsltStylesheetPtr cur = NULL;
|
xsltStylesheetPtr cur = NULL;
|
||||||
xmlDocPtr doc, res;
|
xmlDocPtr doc, res;
|
||||||
|
|
||||||
@ -51,11 +57,33 @@ main(int argc, char **argv) {
|
|||||||
return(1);
|
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);
|
xmlSubstituteEntitiesDefault(1);
|
||||||
xmlLoadExtDtdDefaultValue = 1;
|
xmlLoadExtDtdDefaultValue = 1;
|
||||||
cur = xsltParseStylesheetFile((const xmlChar *)argv[1]);
|
cur = xsltParseStylesheetFile((const xmlChar *)argv[i]);
|
||||||
doc = xmlParseFile(argv[2]);
|
i++;
|
||||||
res = xsltApplyStylesheet(cur, doc, NULL);
|
doc = xmlParseFile(argv[i]);
|
||||||
|
res = xsltApplyStylesheet(cur, doc, params);
|
||||||
xsltSaveResultToFile(stdout, res, cur);
|
xsltSaveResultToFile(stdout, res, cur);
|
||||||
|
|
||||||
xsltFreeStylesheet(cur);
|
xsltFreeStylesheet(cur);
|
||||||
|
@ -3,18 +3,17 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
|
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
|
||||||
<title>libxslt Tutorial</title>
|
<title>libxslt Tutorial</title>
|
||||||
<meta name="generator" content="DocBook XSL Stylesheets V1.29">
|
<meta name="generator" content="DocBook XSL Stylesheets V1.41">
|
||||||
</head>
|
</head>
|
||||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div id="id2741878" class="article">
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="article">
|
||||||
<div class="titlepage">
|
<div class="titlepage">
|
||||||
<div><h1 class="title">
|
<div><h1 class="title">
|
||||||
<a name="id2741878"></a>libxslt Tutorial</h1></div>
|
<a name="id2702588"></a>libxslt Tutorial</h1></div>
|
||||||
<div><h3 class="author">John Fleck</h3></div>
|
<div><h3 class="author">John Fleck</h3></div>
|
||||||
<div><span class="releaseinfo">
|
<div><p class="releaseinfo">
|
||||||
This is version 0.2 of the libxslt Tutorial
|
This is version 0.3 of the libxslt Tutorial
|
||||||
<br>
|
</p></div>
|
||||||
</span></div>
|
<div><p class="copyright">Copyright <20> 2001 John Fleck</p></div>
|
||||||
<div><p class="copyright">Copyright <20> 2001 by John Fleck</p></div>
|
|
||||||
<div><div class="legalnotice"><p>Permission is granted to copy, distribute and/or modify this
|
<div><div class="legalnotice"><p>Permission is granted to copy, distribute and/or modify this
|
||||||
document under the terms of the <i>GNU Free Documentation
|
document under the terms of the <i>GNU Free Documentation
|
||||||
License</i>, Version 1.1 or any later version
|
License</i>, Version 1.1 or any later version
|
||||||
@ -41,15 +40,18 @@
|
|||||||
</dt>
|
</dt>
|
||||||
<dt> <a href="#saveresult">Saving the result</a>
|
<dt> <a href="#saveresult">Saving the result</a>
|
||||||
</dt>
|
</dt>
|
||||||
|
<dt> <a href="#parameters">Parameters</a>
|
||||||
|
</dt>
|
||||||
<dt> <a href="#cleanup">Cleanup</a>
|
<dt> <a href="#cleanup">Cleanup</a>
|
||||||
</dt>
|
</dt>
|
||||||
</dl></dd>
|
</dl></dd>
|
||||||
|
<dt>A <a href="#thecode">The Code</a>
|
||||||
|
</dt>
|
||||||
</dl>
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
<div class="abstract">
|
<div class="abstract">
|
||||||
<p>
|
<p>
|
||||||
<a name="id2692706"></a>
|
<a name="id2708071"></a><b>Abstract</b>
|
||||||
<b>Abstract</b>
|
|
||||||
</p>
|
</p>
|
||||||
<p>A tutorial on building a simple application using the
|
<p>A tutorial on building a simple application using the
|
||||||
libxslt library to perform
|
libxslt library to perform
|
||||||
@ -57,11 +59,8 @@
|
|||||||
XML file into HTML.</p>
|
XML file into HTML.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sect1">
|
<div class="sect1">
|
||||||
<a name="introduction"></a>
|
<a name="introduction"></a><div class="titlepage"><div><h2 class="title" style="clear: both">
|
||||||
<div class="titlepage"><div><h2 class="title" style="clear: all">
|
<a name="introduction"></a>Introduction</h2></div></div>
|
||||||
<a name="introduction"></a>
|
|
||||||
<span class="title">Introduction</span>
|
|
||||||
</h2></div></div>
|
|
||||||
<p>The Extensible Markup Language (XML) is a World
|
<p>The Extensible Markup Language (XML) is a World
|
||||||
Wide Web Consortium standard for the exchange of structured data in text
|
Wide Web Consortium standard for the exchange of structured data in text
|
||||||
form. Its popularity stems from its universality. Any computer can
|
form. Its popularity stems from its universality. Any computer can
|
||||||
@ -81,7 +80,8 @@
|
|||||||
transformations.
|
transformations.
|
||||||
|
|
||||||
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
|
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
|
||||||
<h3 class="title"><a name="id2741924">Note</a></h3>
|
<h3 class="title">
|
||||||
|
<a name="id2754806"></a>Note</h3>
|
||||||
<p>
|
<p>
|
||||||
While libxslt was written
|
While libxslt was written
|
||||||
under the auspices of the GNOME project, it does not
|
under the auspices of the GNOME project, it does not
|
||||||
@ -108,45 +108,55 @@
|
|||||||
<p>References:
|
<p>References:
|
||||||
<div class="itemizedlist"><ul>
|
<div class="itemizedlist"><ul>
|
||||||
<li><p>
|
<li><p>
|
||||||
<a name="id2691214"></a>
|
<a name="id2754758"></a><a href="http://www.w3.org/XML/" target="_top">W3C XML page</a>
|
||||||
<a href="http://www.w3.org/XML/" target="_top">W3C XML page</a>
|
|
||||||
</p></li>
|
</p></li>
|
||||||
<li><p>
|
<li><p>
|
||||||
<a name="id2691235"></a>
|
<a name="id2705552"></a><a href="http://www.w3.org/Style/XSL/" target="_top">W3C
|
||||||
<a href="http://www.w3.org/Style/XSL/" target="_top">W3C
|
|
||||||
XSL page.</a>
|
XSL page.</a>
|
||||||
</p></li>
|
</p></li>
|
||||||
<li><p>
|
<li><p>
|
||||||
<a name="id2691256"></a>
|
<a name="id2705573"></a><a href="http://xmlsoft.org/XSLT/" target="_top">libxslt</a>
|
||||||
<a href="http://xmlsoft.org/XSLT/" target="_top">libxslt</a>
|
|
||||||
</p></li>
|
</p></li>
|
||||||
</ul></div>
|
</ul></div>
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sect1">
|
<div class="sect1">
|
||||||
<a name="functions"></a>
|
<a name="functions"></a><div class="titlepage"><div><h2 class="title" style="clear: both">
|
||||||
<div class="titlepage"><div><h2 class="title" style="clear: all">
|
<a name="functions"></a>Primary Functions</h2></div></div>
|
||||||
<a name="functions"></a>
|
<div class="toc">
|
||||||
<span class="title">Primary Functions</span>
|
<p><b>Table of Contents</b></p>
|
||||||
</h2></div></div>
|
<dl>
|
||||||
|
<dt> <a href="#preparing">Preparing to Parse</a>
|
||||||
|
</dt>
|
||||||
|
<dt> <a href="#parsethestylesheet">Parse the Stylesheet</a>
|
||||||
|
</dt>
|
||||||
|
<dt> <a href="#parseinputfile">Parse the Input File</a>
|
||||||
|
</dt>
|
||||||
|
<dt> <a href="#applyingstylesheet">Applying the Stylesheet</a>
|
||||||
|
</dt>
|
||||||
|
<dt> <a href="#saveresult">Saving the result</a>
|
||||||
|
</dt>
|
||||||
|
<dt> <a href="#parameters">Parameters</a>
|
||||||
|
</dt>
|
||||||
|
<dt> <a href="#cleanup">Cleanup</a>
|
||||||
|
</dt>
|
||||||
|
</dl>
|
||||||
|
</div>
|
||||||
<p>To transform an XML file, you must perform three
|
<p>To transform an XML file, you must perform three
|
||||||
functions:
|
functions:
|
||||||
<div class="orderedlist"><ol type="1">
|
<div class="orderedlist"><ol type="1">
|
||||||
<li><p>
|
<li><p>
|
||||||
<a name="id2691302"></a>parse the input file</p></li>
|
<a name="id2705618"></a>parse the input file</p></li>
|
||||||
<li><p>
|
<li><p>
|
||||||
<a name="id2691310"></a>parse the stylesheet</p></li>
|
<a name="id2705627"></a>parse the stylesheet</p></li>
|
||||||
<li><p>
|
<li><p>
|
||||||
<a name="id2691319"></a>apply the stylesheet</p></li>
|
<a name="id2705635"></a>apply the stylesheet</p></li>
|
||||||
</ol></div>
|
</ol></div>
|
||||||
</p>
|
</p>
|
||||||
<div class="sect2">
|
<div class="sect2">
|
||||||
<a name="preparing"></a>
|
<a name="preparing"></a><div class="titlepage"><div><h3 class="title">
|
||||||
<div class="titlepage"><div><h3 class="title">
|
<a name="preparing"></a>Preparing to Parse</h3></div></div>
|
||||||
<a name="preparing"></a>
|
|
||||||
<span class="title">Preparing to Parse</span>
|
|
||||||
</h3></div></div>
|
|
||||||
<p>Before you can begin parsing input files or stylesheets, there are
|
<p>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
|
several steps you need to take to set up entity handling. These steps are
|
||||||
not unique to libxslt. Any
|
not unique to libxslt. Any
|
||||||
@ -162,20 +172,17 @@
|
|||||||
</p>
|
</p>
|
||||||
<p>Second, set <tt>xmlLoadExtDtdDefaultValue</tt> equal to
|
<p>Second, set <tt>xmlLoadExtDtdDefaultValue</tt> equal to
|
||||||
<i><tt>1</tt></i>. This tells libxml
|
<i><tt>1</tt></i>. 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
|
input file includes entities through external subsets, you will get
|
||||||
errors.</p>
|
errors.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sect2">
|
<div class="sect2">
|
||||||
<a name="parsethestylesheet"></a>
|
<a name="parsethestylesheet"></a><div class="titlepage"><div><h3 class="title">
|
||||||
<div class="titlepage"><div><h3 class="title">
|
<a name="parsethestylesheet"></a>Parse the Stylesheet</h3></div></div>
|
||||||
<a name="parsethestylesheet"></a>
|
|
||||||
<span class="title">Parse the Stylesheet</span>
|
|
||||||
</h3></div></div>
|
|
||||||
<p>Parsing the stylesheet takes a single function call, which takes a
|
<p>Parsing the stylesheet takes a single function call, which takes a
|
||||||
variable of type xmlChar:
|
variable of type xmlChar:
|
||||||
<pre class="programlisting">
|
<pre class="programlisting">
|
||||||
<tt>cur</tt> = xsltParseStylesheetFile((const xmlChar *)argv[1]);
|
<tt>cur</tt> = xsltParseStylesheetFile((const xmlChar *)argv[i]);
|
||||||
</pre>
|
</pre>
|
||||||
In this case, I cast the stylesheet file name, passed in as a
|
In this case, I cast the stylesheet file name, passed in as a
|
||||||
command line argument, to <i>xmlChar</i>. The return value
|
command line argument, to <i>xmlChar</i>. The return value
|
||||||
@ -186,14 +193,11 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sect2">
|
<div class="sect2">
|
||||||
<a name="parseinputfile"></a>
|
<a name="parseinputfile"></a><div class="titlepage"><div><h3 class="title">
|
||||||
<div class="titlepage"><div><h3 class="title">
|
<a name="parseinputfile"></a>Parse the Input File</h3></div></div>
|
||||||
<a name="parseinputfile"></a>
|
|
||||||
<span class="title">Parse the Input File</span>
|
|
||||||
</h3></div></div>
|
|
||||||
<p>Parsing the input file takes a single function call:
|
<p>Parsing the input file takes a single function call:
|
||||||
<pre class="programlisting">
|
<pre class="programlisting">
|
||||||
doc = xmlParseFile(argv[2]);
|
doc = xmlParseFile(argv[i]);
|
||||||
</pre>
|
</pre>
|
||||||
It returns an <i>xmlDocPtr</i>, a struct in memory that
|
It returns an <i>xmlDocPtr</i>, a struct in memory that
|
||||||
contains the document tree. It can be manipulated directly, but for this
|
contains the document tree. It can be manipulated directly, but for this
|
||||||
@ -201,30 +205,24 @@ doc = xmlParseFile(argv[2]);
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sect2">
|
<div class="sect2">
|
||||||
<a name="applyingstylesheet"></a>
|
<a name="applyingstylesheet"></a><div class="titlepage"><div><h3 class="title">
|
||||||
<div class="titlepage"><div><h3 class="title">
|
<a name="applyingstylesheet"></a>Applying the Stylesheet</h3></div></div>
|
||||||
<a name="applyingstylesheet"></a>
|
|
||||||
<span class="title">Applying the Stylesheet</span>
|
|
||||||
</h3></div></div>
|
|
||||||
<p>Now that you have trees representing the document and the stylesheet
|
<p>Now that you have trees representing the document and the stylesheet
|
||||||
in memory, apply the stylesheet to the document. The
|
in memory, apply the stylesheet to the document. The
|
||||||
function that does this is <tt>xsltApplyStylesheet</tt>:
|
function that does this is <tt>xsltApplyStylesheet</tt>:
|
||||||
<pre class="programlisting">
|
<pre class="programlisting">
|
||||||
res = xsltApplyStylesheet(cur, doc, NULL);
|
res = xsltApplyStylesheet(cur, doc, params);
|
||||||
</pre>
|
</pre>
|
||||||
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
|
xmlDocPtr, the values returned by the previous two functions. The third
|
||||||
parameter, NULL in this case, can be used to pass parameters to the
|
variable, <tt>params</tt> can be used to pass
|
||||||
stylesheet. It is a NULL-terminated array of name/value pairs of const
|
XSLT parameters to the stylesheet. It is a
|
||||||
char's.
|
NULL-terminated array of name/value pairs of const char's.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sect2">
|
<div class="sect2">
|
||||||
<a name="saveresult"></a>
|
<a name="saveresult"></a><div class="titlepage"><div><h3 class="title">
|
||||||
<div class="titlepage"><div><h3 class="title">
|
<a name="saveresult"></a>Saving the result</h3></div></div>
|
||||||
<a name="saveresult"></a>
|
|
||||||
<span class="title">Saving the result</span>
|
|
||||||
</h3></div></div>
|
|
||||||
<p>libxslt includes a family of functions to use in
|
<p>libxslt includes a family of functions to use in
|
||||||
saving the resulting output. For this example,
|
saving the resulting output. For this example,
|
||||||
<tt>xsltSaveResultToFile</tt> is used, and the results are
|
<tt>xsltSaveResultToFile</tt> is used, and the results are
|
||||||
@ -235,7 +233,8 @@ xsltSaveResultToFile(stdout, res, cur);
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
|
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
|
||||||
<h3 class="title"><a name="id2691572">Note</a></h3>
|
<h3 class="title">
|
||||||
|
<a name="id2708523"></a>Note</h3>
|
||||||
<p>libxml also contains output
|
<p>libxml also contains output
|
||||||
functions, such as <tt>xmlSaveFile</tt>, which can be
|
functions, such as <tt>xmlSaveFile</tt>, which can be
|
||||||
used here. However, output-related information contained in the
|
used here. However, output-related information contained in the
|
||||||
@ -246,11 +245,39 @@ xsltSaveResultToFile(stdout, res, cur);
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="sect2">
|
<div class="sect2">
|
||||||
<a name="cleanup"></a>
|
<a name="parameters"></a><div class="titlepage"><div><h3 class="title">
|
||||||
<div class="titlepage"><div><h3 class="title">
|
<a name="parameters"></a>Parameters</h3></div></div>
|
||||||
<a name="cleanup"></a>
|
<p>
|
||||||
<span class="title">Cleanup</span>
|
In XSLT, parameters may be used as a way to pass
|
||||||
</h3></div></div>
|
additional information to a
|
||||||
|
stylesheet. libxslt accepts
|
||||||
|
XSLT parameters as one of the values passed to
|
||||||
|
<tt>xsltApplyStylesheet</tt>.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
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 <tt>params</tt>, then
|
||||||
|
passes them to the function. The final element in the array is set to
|
||||||
|
<i><tt>NULL</tt></i>.
|
||||||
|
|
||||||
|
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
|
||||||
|
<h3 class="title">
|
||||||
|
<a name="id2708604"></a>Note</h3>
|
||||||
|
<p>
|
||||||
|
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:
|
||||||
|
<b>tutorial]$ ./libxslt_tutorial --param rootid "'asect1'"
|
||||||
|
stylesheet.xsl filename.xml</b>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div class="sect2">
|
||||||
|
<a name="cleanup"></a><div class="titlepage"><div><h3 class="title">
|
||||||
|
<a name="cleanup"></a>Cleanup</h3></div></div>
|
||||||
<p>After you are finished, libxslt and
|
<p>After you are finished, libxslt and
|
||||||
libxml provide functions for deallocating
|
libxml provide functions for deallocating
|
||||||
memory.
|
memory.
|
||||||
@ -263,29 +290,31 @@ xsltSaveResultToFile(stdout, res, cur);
|
|||||||
xmlFreeDoc(doc);<a name="cleanupdoc"></a><img src="../images/callouts/3.png" alt="3" border="0">
|
xmlFreeDoc(doc);<a name="cleanupdoc"></a><img src="../images/callouts/3.png" alt="3" border="0">
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<div class="calloutlist"><table border="0" summary="Callout list">
|
<div class="calloutlist">
|
||||||
|
<a name="id2708906"></a><table border="0" summary="Callout list">
|
||||||
<tr>
|
<tr>
|
||||||
<td width="5%" valign="top" align="left">
|
<td width="5%" valign="top" align="left">
|
||||||
<a href="#cleanupstylesheet"><img src="../images/callouts/1.png" alt="1" border="0"></a> </td>
|
<a name="id2708911"></a><a href="#cleanupstylesheet"><img src="../images/callouts/1.png" alt="1" border="0"></a> </td>
|
||||||
<td valign="top" align="left"><p>Free the memory used by your stylesheet.</p></td>
|
<td valign="top" align="left"><p>Free the memory used by your stylesheet.</p></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td width="5%" valign="top" align="left">
|
<td width="5%" valign="top" align="left">
|
||||||
<a href="#cleanupresults"><img src="../images/callouts/2.png" alt="2" border="0"></a> </td>
|
<a name="id2709027"></a><a href="#cleanupresults"><img src="../images/callouts/2.png" alt="2" border="0"></a> </td>
|
||||||
<td valign="top" align="left"><p>Free the memory used by the results document.</p></td>
|
<td valign="top" align="left"><p>Free the memory used by the results document.</p></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td width="5%" valign="top" align="left">
|
<td width="5%" valign="top" align="left">
|
||||||
<a href="#cleanupdoc"><img src="../images/callouts/3.png" alt="3" border="0"></a> </td>
|
<a name="id2709045"></a><a href="#cleanupdoc"><img src="../images/callouts/3.png" alt="3" border="0"></a> </td>
|
||||||
<td valign="top" align="left"><p>Free the memory used by your original document.</p></td>
|
<td valign="top" align="left"><p>Free the memory used by your original document.</p></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table></div>
|
</table>
|
||||||
|
</div>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="thecode" class="appendix">
|
<div class="appendix">
|
||||||
<h2 class="title" style="clear: all">
|
<h2 class="title" style="clear: both">
|
||||||
<a name="thecode"></a>Appendix A. The Code</h2>
|
<a name="thecode"></a>A. The Code</h2>
|
||||||
<p>
|
<p>
|
||||||
<tt>libxslt_tutorial.c</tt>
|
<tt>libxslt_tutorial.c</tt>
|
||||||
<pre class="programlisting">
|
<pre class="programlisting">
|
||||||
@ -311,6 +340,7 @@ xsltSaveResultToFile(stdout, res, cur);
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
#include <libxml/xmlmemory.h>
|
#include <libxml/xmlmemory.h>
|
||||||
#include <libxml/debugXML.h>
|
#include <libxml/debugXML.h>
|
||||||
#include <libxml/HTMLtree.h>
|
#include <libxml/HTMLtree.h>
|
||||||
@ -329,10 +359,15 @@ extern int xmlLoadExtDtdDefaultValue;
|
|||||||
|
|
||||||
static void usage(const char *name) {
|
static void usage(const char *name) {
|
||||||
printf("Usage: %s [options] stylesheet file [file ...]\n", name);
|
printf("Usage: %s [options] stylesheet file [file ...]\n", name);
|
||||||
|
printf(" --param name value : pass a (parameter,value) pair\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv) {
|
main(int argc, char **argv) {
|
||||||
|
int i;
|
||||||
|
const char *params[16 + 1];
|
||||||
|
int nbparams = 0;
|
||||||
xsltStylesheetPtr cur = NULL;
|
xsltStylesheetPtr cur = NULL;
|
||||||
xmlDocPtr doc, res;
|
xmlDocPtr doc, res;
|
||||||
|
|
||||||
@ -341,11 +376,33 @@ main(int argc, char **argv) {
|
|||||||
return(1);
|
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);
|
xmlSubstituteEntitiesDefault(1);
|
||||||
xmlLoadExtDtdDefaultValue = 1;
|
xmlLoadExtDtdDefaultValue = 1;
|
||||||
cur = xsltParseStylesheetFile((const xmlChar *)argv[1]);
|
cur = xsltParseStylesheetFile((const xmlChar *)argv[i]);
|
||||||
doc = xmlParseFile(argv[2]);
|
i++;
|
||||||
res = xsltApplyStylesheet(cur, doc, NULL);
|
doc = xmlParseFile(argv[i]);
|
||||||
|
res = xsltApplyStylesheet(cur, doc, params);
|
||||||
xsltSaveResultToFile(stdout, res, cur);
|
xsltSaveResultToFile(stdout, res, cur);
|
||||||
|
|
||||||
xsltFreeStylesheet(cur);
|
xsltFreeStylesheet(cur);
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
<surname>Fleck</surname>
|
<surname>Fleck</surname>
|
||||||
</author>
|
</author>
|
||||||
<releaseinfo>
|
<releaseinfo>
|
||||||
This is version 0.2 of the libxslt Tutorial
|
This is version 0.3 of the libxslt Tutorial
|
||||||
</releaseinfo>
|
</releaseinfo>
|
||||||
</articleinfo>
|
</articleinfo>
|
||||||
<abstract>
|
<abstract>
|
||||||
@ -136,7 +136,7 @@
|
|||||||
|
|
||||||
<para>Second, set <varname>xmlLoadExtDtdDefaultValue</varname> equal to
|
<para>Second, set <varname>xmlLoadExtDtdDefaultValue</varname> equal to
|
||||||
<parameter>1</parameter>. This tells <application>libxml</application>
|
<parameter>1</parameter>. This tells <application>libxml</application>
|
||||||
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
|
input file includes entities through external subsets, you will get
|
||||||
errors.</para>
|
errors.</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
@ -145,7 +145,7 @@
|
|||||||
<para>Parsing the stylesheet takes a single function call, which takes a
|
<para>Parsing the stylesheet takes a single function call, which takes a
|
||||||
variable of type <type>xmlChar</type>:
|
variable of type <type>xmlChar</type>:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
<varname>cur</varname> = xsltParseStylesheetFile((const xmlChar *)argv[1]);
|
<varname>cur</varname> = xsltParseStylesheetFile((const xmlChar *)argv[i]);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
In this case, I cast the stylesheet file name, passed in as a
|
In this case, I cast the stylesheet file name, passed in as a
|
||||||
command line argument, to <emphasis>xmlChar</emphasis>. The return value
|
command line argument, to <emphasis>xmlChar</emphasis>. The return value
|
||||||
@ -160,7 +160,7 @@
|
|||||||
<title>Parse the Input File</title>
|
<title>Parse the Input File</title>
|
||||||
<para>Parsing the input file takes a single function call:
|
<para>Parsing the input file takes a single function call:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
doc = xmlParseFile(argv[2]);
|
doc = xmlParseFile(argv[i]);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
It returns an <emphasis>xmlDocPtr</emphasis>, a struct in memory that
|
It returns an <emphasis>xmlDocPtr</emphasis>, a struct in memory that
|
||||||
contains the document tree. It can be manipulated directly, but for this
|
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
|
in memory, apply the stylesheet to the document. The
|
||||||
function that does this is <function>xsltApplyStylesheet</function>:
|
function that does this is <function>xsltApplyStylesheet</function>:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
res = xsltApplyStylesheet(cur, doc, NULL);
|
res = xsltApplyStylesheet(cur, doc, params);
|
||||||
</programlisting>
|
</programlisting>
|
||||||
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
|
xmlDocPtr, the values returned by the previous two functions. The third
|
||||||
parameter, NULL in this case, can be used to pass parameters to the
|
variable, <varname>params</varname> can be used to pass
|
||||||
stylesheet. It is a NULL-terminated array of name/value pairs of const
|
<acronym>XSLT</acronym> parameters to the stylesheet. It is a
|
||||||
char's.
|
NULL-terminated array of name/value pairs of const char's.
|
||||||
</para>
|
</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
|
|
||||||
@ -206,6 +206,37 @@ xsltSaveResultToFile(stdout, res, cur);
|
|||||||
</para>
|
</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
|
|
||||||
|
<sect2 id="parameters">
|
||||||
|
<title>Parameters</title>
|
||||||
|
<para>
|
||||||
|
In <acronym>XSLT</acronym>, parameters may be used as a way to pass
|
||||||
|
additional information to a
|
||||||
|
stylesheet. <application>libxslt</application> accepts
|
||||||
|
<acronym>XSLT</acronym> parameters as one of the values passed to
|
||||||
|
<function>xsltApplyStylesheet</function>.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>
|
||||||
|
In the tutorial example and in <application>xsltproc</application>,
|
||||||
|
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 <varname>params</varname>, then
|
||||||
|
passes them to the function. The final element in the array is set to
|
||||||
|
<parameter>NULL</parameter>.
|
||||||
|
|
||||||
|
<note>
|
||||||
|
<para>
|
||||||
|
If a parameter being passed is a string rather than an
|
||||||
|
<acronym>XSLT</acronym> node, it must be escaped. For the tutorial
|
||||||
|
program, that would be done as follows:
|
||||||
|
<command>tutorial]$ ./libxslt_tutorial --param rootid "'asect1'"
|
||||||
|
stylesheet.xsl filename.xml</command>
|
||||||
|
</para>
|
||||||
|
</note>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
</sect2>
|
||||||
|
|
||||||
<sect2 id="cleanup">
|
<sect2 id="cleanup">
|
||||||
<title>Cleanup</title>
|
<title>Cleanup</title>
|
||||||
<para>After you are finished, <application>libxslt</application> and
|
<para>After you are finished, <application>libxslt</application> and
|
||||||
|
Reference in New Issue
Block a user