VirtualBox

source: vbox/trunk/src/libs/libxslt-1.1.22/doc/tutorial/libxslttutorial.html@ 7979

Last change on this file since 7979 was 7296, checked in by vboxsync, 17 years ago

Added libxslt-1.1.22 sources.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 15.1 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2<html>
3<head>
4<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
5<title>libxslt Tutorial</title>
6<meta name="generator" content="DocBook XSL Stylesheets V1.41">
7</head>
8<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="article">
9<div class="titlepage">
10<div><h1 class="title">
11<a name="id2702654"></a>libxslt Tutorial</h1></div>
12<div><h3 class="author">John Fleck</h3></div>
13<div><p class="releaseinfo">
14 This is version 0.4 of the libxslt Tutorial
15 </p></div>
16<div><p class="copyright">Copyright © 2001 John Fleck</p></div>
17<div><div class="legalnotice"><p>Permission is granted to copy, distribute and/or modify this
18 document under the terms of the <i>GNU Free Documentation
19 License</i>, Version 1.1 or any later version
20 published by the Free Software Foundation with no Invariant
21 Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of
22 the license can be found <a href="http://www.gnu.org/copyleft/fdl.html" target="_top">here</a>.</p></div></div>
23<hr>
24</div>
25<div class="toc">
26<p><b>Table of Contents</b></p>
27<dl>
28<dt> <a href="#introduction">Introduction</a>
29</dt>
30<dt> <a href="#functions">Primary Functions</a>
31</dt>
32<dd><dl>
33<dt> <a href="#preparing">Preparing to Parse</a>
34</dt>
35<dt> <a href="#parsethestylesheet">Parse the Stylesheet</a>
36</dt>
37<dt> <a href="#parseinputfile">Parse the Input File</a>
38</dt>
39<dt> <a href="#applyingstylesheet">Applying the Stylesheet</a>
40</dt>
41<dt> <a href="#saveresult">Saving the result</a>
42</dt>
43<dt> <a href="#parameters">Parameters</a>
44</dt>
45<dt> <a href="#cleanup">Cleanup</a>
46</dt>
47</dl></dd>
48<dt>A <a href="#thecode">The Code</a>
49</dt>
50</dl>
51</div>
52<div class="abstract">
53<p>
54<a name="id2705766"></a><b>Abstract</b>
55</p>
56<p>A tutorial on building a simple application using the
57 libxslt library to perform
58 XSLT transformations to convert an
59 XML file into HTML.</p>
60</div>
61<div class="sect1">
62<a name="introduction"></a><div class="titlepage"><div><h2 class="title" style="clear: both">
63<a name="introduction"></a>Introduction</h2></div></div>
64<p>The Extensible Markup Language (XML) is a World
65 Wide Web Consortium standard for the exchange of structured data in text
66 form. Its popularity stems from its universality. Any computer can
67 read a text file. With the proper tools, any computer can read any other
68 computer's XML files.
69 </p>
70<p>One of the most important of those tools is XSLT:
71 Extensible Stylesheet Language Transformations. XSLT
72 is a declarative language that allows you to
73 translate your XML into arbitrary text output
74 using a stylesheet. libxslt provides the
75 functions to perform the transformation.
76 </p>
77<p>libxslt is a free C language library
78 written by Daniel Veillard for the GNOME project
79 allowing you to write programs that perform XSLT
80 transformations.
81
82 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
83<h3 class="title">
84<a name="id2754803"></a>Note</h3>
85<p>
86 While libxslt was written
87 under the auspices of the GNOME project, it does not
88 depend on any GNOME libraries. None are used in the
89 example in this tutorial.
90 </p>
91</div>
92
93 </p>
94<p>This tutorial illustrates a simple program that reads an
95 XML file, applies a stylesheet and saves the resulting
96 output. This is not a program you would want to create
97 yourself. xsltproc, which is included with the
98 libxslt package, does the same thing and is
99 more robust and full-featured. The program written for this tutorial is a
100 stripped-down version of xsltproc designed to
101 illustrate the functionality of libxslt.
102 </p>
103<p>The full code for xsltproc is in
104 <tt>xsltproc.c</tt> in the libxslt
105 distribution. It also is available <a href="http://cvs.gnome.org/lxr/source/libxslt/libxslt/xsltproc.c" target="_top">on the
106 web</a>.
107 </p>
108<p>References:
109 <div class="itemizedlist"><ul>
110<li><p>
111<a name="id2708005"></a><a href="http://www.w3.org/XML/" target="_top">W3C XML page</a>
112</p></li>
113<li><p>
114<a name="id2708026"></a><a href="http://www.w3.org/Style/XSL/" target="_top">W3C
115 XSL page.</a>
116</p></li>
117<li><p>
118<a name="id2708047"></a><a href="http://xmlsoft.org/XSLT/" target="_top">libxslt</a>
119</p></li>
120</ul></div>
121
122 </p>
123</div>
124<div class="sect1">
125<a name="functions"></a><div class="titlepage"><div><h2 class="title" style="clear: both">
126<a name="functions"></a>Primary Functions</h2></div></div>
127<div class="toc">
128<p><b>Table of Contents</b></p>
129<dl>
130<dt> <a href="#preparing">Preparing to Parse</a>
131</dt>
132<dt> <a href="#parsethestylesheet">Parse the Stylesheet</a>
133</dt>
134<dt> <a href="#parseinputfile">Parse the Input File</a>
135</dt>
136<dt> <a href="#applyingstylesheet">Applying the Stylesheet</a>
137</dt>
138<dt> <a href="#saveresult">Saving the result</a>
139</dt>
140<dt> <a href="#parameters">Parameters</a>
141</dt>
142<dt> <a href="#cleanup">Cleanup</a>
143</dt>
144</dl>
145</div>
146<p>To transform an XML file, you must perform three
147 functions:
148 <div class="orderedlist"><ol type="1">
149<li><p>
150<a name="id2708093"></a>parse the input file</p></li>
151<li><p>
152<a name="id2708101"></a>parse the stylesheet</p></li>
153<li><p>
154<a name="id2708110"></a>apply the stylesheet</p></li>
155</ol></div>
156 </p>
157<div class="sect2">
158<a name="preparing"></a><div class="titlepage"><div><h3 class="title">
159<a name="preparing"></a>Preparing to Parse</h3></div></div>
160<p>Before you can begin parsing input files or stylesheets, there are
161 several steps you need to take to set up entity handling. These steps are
162 not unique to libxslt. Any
163 libxml2 program that parses
164 XML files would need to take similar steps.
165 </p>
166<p>First, you need set up some libxml
167 housekeeping. Pass the integer value <i><tt>1</tt></i> to the
168 <tt>xmlSubstituteEntitiesDefault</tt> function, which tells
169 the libxml2 parser to substitute entities as
170 it parses your file. (Passing <i><tt>0</tt></i> causes
171 libxml2 to not perform entity substitution.)
172 </p>
173<p>Second, set <tt>xmlLoadExtDtdDefaultValue</tt> equal to
174 <i><tt>1</tt></i>. This tells libxml
175 to load external entity subsets. If you do not do this and your
176 input file includes entities through external subsets, you will get
177 errors.</p>
178</div>
179<div class="sect2">
180<a name="parsethestylesheet"></a><div class="titlepage"><div><h3 class="title">
181<a name="parsethestylesheet"></a>Parse the Stylesheet</h3></div></div>
182<p>Parsing the stylesheet takes a single function call, which takes a
183 variable of type xmlChar:
184 <pre class="programlisting">
185 <tt>cur</tt> = xsltParseStylesheetFile((const xmlChar *)argv[i]);
186 </pre>
187 In this case, I cast the stylesheet file name, passed in as a
188 command line argument, to <i>xmlChar</i>. The return value
189 is of type <i>xsltStylesheetPtr</i>, a struct in memory
190 that contains the stylesheet tree and other information about the
191 stylesheet. It can be manipulated directly, but for this example you
192 will not need to.
193 </p>
194</div>
195<div class="sect2">
196<a name="parseinputfile"></a><div class="titlepage"><div><h3 class="title">
197<a name="parseinputfile"></a>Parse the Input File</h3></div></div>
198<p>Parsing the input file takes a single function call:
199 <pre class="programlisting">
200doc = xmlParseFile(argv[i]);
201 </pre>
202 It returns an <i>xmlDocPtr</i>, a struct in memory that
203 contains the document tree. It can be manipulated directly, but for this
204 example you will not need to.
205 </p>
206</div>
207<div class="sect2">
208<a name="applyingstylesheet"></a><div class="titlepage"><div><h3 class="title">
209<a name="applyingstylesheet"></a>Applying the Stylesheet</h3></div></div>
210<p>Now that you have trees representing the document and the stylesheet
211 in memory, apply the stylesheet to the document. The
212 function that does this is <tt>xsltApplyStylesheet</tt>:
213 <pre class="programlisting">
214res = xsltApplyStylesheet(cur, doc, params);
215 </pre>
216 The function takes an xsltStylesheetPtr and an
217 xmlDocPtr, the values returned by the previous two functions. The third
218 variable, <tt>params</tt> can be used to pass
219 XSLT parameters to the stylesheet. It is a
220 NULL-terminated array of name/value pairs of const char's.
221 </p>
222</div>
223<div class="sect2">
224<a name="saveresult"></a><div class="titlepage"><div><h3 class="title">
225<a name="saveresult"></a>Saving the result</h3></div></div>
226<p>libxslt includes a family of functions to use in
227 saving the resulting output. For this example,
228 <tt>xsltSaveResultToFile</tt> is used, and the results are
229 saved to stdout:
230
231 <pre class="programlisting">
232xsltSaveResultToFile(stdout, res, cur);
233 </pre>
234
235 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
236<h3 class="title">
237<a name="id2708587"></a>Note</h3>
238<p>libxml also contains output
239 functions, such as <tt>xmlSaveFile</tt>, which can be
240 used here. However, output-related information contained in the
241 stylesheet, such as a declaration of the encoding to be used, will
242 be lost if one of the libxslt save
243 functions is not used.</p>
244</div>
245 </p>
246</div>
247<div class="sect2">
248<a name="parameters"></a><div class="titlepage"><div><h3 class="title">
249<a name="parameters"></a>Parameters</h3></div></div>
250<p>
251 In XSLT, parameters may be used as a way to pass
252 additional information to a
253 stylesheet. libxslt accepts
254 XSLT parameters as one of the values passed to
255 <tt>xsltApplyStylesheet</tt>.
256 </p>
257<p>
258 In the tutorial example and in xsltproc,
259 on which the tutorial example is based, parameters to be passed take the
260 form of key-value pairs. The program collects them from command line
261 arguments, inserting them in the array <tt>params</tt>, then
262 passes them to the function. The final element in the array is set to
263 <i><tt>NULL</tt></i>.
264
265 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
266<h3 class="title">
267<a name="id2708668"></a>Note</h3>
268<p>
269 If a parameter being passed is a string rather than an
270 XSLT node, it must be escaped. For the tutorial
271 program, that would be done as follows:
272 <b>tutorial]$ ./libxslt_tutorial --param rootid &quot;'asect1'&quot;
273 stylesheet.xsl filename.xml</b>
274 </p>
275</div>
276 </p>
277</div>
278<div class="sect2">
279<a name="cleanup"></a><div class="titlepage"><div><h3 class="title">
280<a name="cleanup"></a>Cleanup</h3></div></div>
281<p>After you are finished, libxslt and
282 libxml provide functions for deallocating
283 memory.
284 </p>
285<p>
286
287 <pre class="programlisting">
288 xsltFreeStylesheet(cur);<a name="cleanupstylesheet"></a><img src="../images/callouts/1.png" alt="1" border="0">
289 xmlFreeDoc(res);<a name="cleanupresults"></a><img src="../images/callouts/2.png" alt="2" border="0">
290 xmlFreeDoc(doc);<a name="cleanupdoc"></a><img src="../images/callouts/3.png" alt="3" border="0">
291 xsltCleanupGlobals();<a name="cleanupglobals"></a><img src="../images/callouts/4.png" alt="4" border="0">
292 xmlCleanupParser();<a name="cleanupparser"></a><img src="../images/callouts/5.png" alt="5" border="0">
293
294 </pre>
295
296 <div class="calloutlist">
297<a name="id2708994"></a><table border="0" summary="Callout list">
298<tr>
299<td width="5%" valign="top" align="left">
300<a name="id2709000"></a><a href="#cleanupstylesheet"><img src="../images/callouts/1.png" alt="1" border="0"></a> </td>
301<td valign="top" align="left"><p>Free the memory used by your stylesheet.</p></td>
302</tr>
303<tr>
304<td width="5%" valign="top" align="left">
305<a name="id2709117"></a><a href="#cleanupresults"><img src="../images/callouts/2.png" alt="2" border="0"></a> </td>
306<td valign="top" align="left"><p>Free the memory used by the results document.</p></td>
307</tr>
308<tr>
309<td width="5%" valign="top" align="left">
310<a name="id2709136"></a><a href="#cleanupdoc"><img src="../images/callouts/3.png" alt="3" border="0"></a> </td>
311<td valign="top" align="left"><p>Free the memory used by your original document.</p></td>
312</tr>
313<tr>
314<td width="5%" valign="top" align="left">
315<a name="id2709155"></a><a href="#cleanupglobals"><img src="../images/callouts/4.png" alt="4" border="0"></a> </td>
316<td valign="top" align="left"><p>Free memory used by libxslt global
317 variables</p></td>
318</tr>
319<tr>
320<td width="5%" valign="top" align="left">
321<a name="id2709176"></a><a href="#cleanupparser"><img src="../images/callouts/5.png" alt="5" border="0"></a> </td>
322<td valign="top" align="left"><p>Free memory used by the XML parser</p></td>
323</tr>
324</table>
325</div>
326 </p>
327</div>
328</div>
329<div class="appendix">
330<h2 class="title" style="clear: both">
331<a name="thecode"></a>A. The Code</h2>
332<p>
333<tt>libxslt_tutorial.c</tt>
334 <pre class="programlisting">
335/*
336 * libxslt_tutorial.c: demo program for the XSL Transformation 1.0 engine
337 *
338 * based on xsltproc.c, by [email protected]
339 * by John Fleck
340 *
341 * This program is free software; you can redistribute it and/or modify
342 * it under the terms of the GNU General Public License as published by
343 * the Free Software Foundation; either version 2 of the License, or
344 * (at your option) any later version.
345 *
346 * This program is distributed in the hope that it will be useful,
347 * but WITHOUT ANY WARRANTY; without even the implied warranty of
348 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
349 * GNU General Public License for more details.
350 *
351 * You should have received a copy of the GNU General Public License
352 * along with this program; if not, write to the Free Software
353 * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
354 *
355 */
356
357#include &lt;string.h&gt;
358#include &lt;libxml/xmlmemory.h&gt;
359#include &lt;libxml/debugXML.h&gt;
360#include &lt;libxml/HTMLtree.h&gt;
361#include &lt;libxml/xmlIO.h&gt;
362#include &lt;libxml/DOCBparser.h&gt;
363#include &lt;libxml/xinclude.h&gt;
364#include &lt;libxml/catalog.h&gt;
365#include &lt;libxslt/xslt.h&gt;
366#include &lt;libxslt/xsltInternals.h&gt;
367#include &lt;libxslt/transform.h&gt;
368#include &lt;libxslt/xsltutils.h&gt;
369
370
371
372extern int xmlLoadExtDtdDefaultValue;
373
374static void usage(const char *name) {
375 printf(&quot;Usage: %s [options] stylesheet file [file ...]\n&quot;, name);
376 printf(&quot; --param name value : pass a (parameter,value) pair\n&quot;);
377
378}
379
380int
381main(int argc, char **argv) {
382 int i;
383 const char *params[16 + 1];
384 int nbparams = 0;
385 xsltStylesheetPtr cur = NULL;
386 xmlDocPtr doc, res;
387
388 if (argc &lt;= 1) {
389 usage(argv[0]);
390 return(1);
391 }
392
393
394 for (i = 1; i &lt; argc; i++) {
395 if (argv[i][0] != '-')
396 break;
397 if ((!strcmp(argv[i], &quot;-param&quot;)) ||
398 (!strcmp(argv[i], &quot;--param&quot;))) {
399 i++;
400 params[nbparams++] = argv[i++];
401 params[nbparams++] = argv[i];
402 if (nbparams &gt;= 16) {
403 fprintf(stderr, &quot;too many params\n&quot;);
404 return (1);
405 }
406 } else {
407 fprintf(stderr, &quot;Unknown option %s\n&quot;, argv[i]);
408 usage(argv[0]);
409 return (1);
410 }
411 }
412
413 params[nbparams] = NULL;
414 xmlSubstituteEntitiesDefault(1);
415 xmlLoadExtDtdDefaultValue = 1;
416 cur = xsltParseStylesheetFile((const xmlChar *)argv[i]);
417 i++;
418 doc = xmlParseFile(argv[i]);
419 res = xsltApplyStylesheet(cur, doc, params);
420 xsltSaveResultToFile(stdout, res, cur);
421
422 xsltFreeStylesheet(cur);
423 xmlFreeDoc(res);
424 xmlFreeDoc(doc);
425
426 xsltCleanupGlobals();
427 xmlCleanupParser();
428 return(0);
429
430}
431
432</pre>
433
434 </p>
435</div>
436</div></body>
437</html>
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette