1 | <?xml version="1.0"?>
|
---|
2 | <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
|
---|
3 | "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
|
---|
4 | <!ENTITY CODE SYSTEM "libxslt_tutorial.c">
|
---|
5 | ]>
|
---|
6 | <article>
|
---|
7 | <articleinfo>
|
---|
8 | <title>libxslt Tutorial</title>
|
---|
9 | <copyright>
|
---|
10 | <year>2001</year>
|
---|
11 | <holder>John Fleck</holder>
|
---|
12 | </copyright>
|
---|
13 | <legalnotice id="legalnotice">
|
---|
14 |
|
---|
15 | <para>Permission is granted to copy, distribute and/or modify this
|
---|
16 | document under the terms of the <citetitle>GNU Free Documentation
|
---|
17 | License</citetitle>, Version 1.1 or any later version
|
---|
18 | published by the Free Software Foundation with no Invariant
|
---|
19 | Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of
|
---|
20 | the license can be found <ulink type="http"
|
---|
21 | url="http://www.gnu.org/copyleft/fdl.html">here</ulink>.</para>
|
---|
22 |
|
---|
23 | </legalnotice>
|
---|
24 | <author>
|
---|
25 | <firstname>John</firstname>
|
---|
26 | <surname>Fleck</surname>
|
---|
27 | </author>
|
---|
28 | <releaseinfo>
|
---|
29 | This is version 0.4 of the libxslt Tutorial
|
---|
30 | </releaseinfo>
|
---|
31 | </articleinfo>
|
---|
32 | <abstract>
|
---|
33 | <para>A tutorial on building a simple application using the
|
---|
34 | <application>libxslt</application> library to perform
|
---|
35 | <acronym>XSLT</acronym> transformations to convert an
|
---|
36 | <acronym>XML</acronym> file into <acronym>HTML</acronym>.</para>
|
---|
37 | </abstract>
|
---|
38 | <sect1 id="introduction">
|
---|
39 | <title>Introduction</title>
|
---|
40 |
|
---|
41 | <para>The Extensible Markup Language (<acronym>XML</acronym>) is a World
|
---|
42 | Wide Web Consortium standard for the exchange of structured data in text
|
---|
43 | form. Its popularity stems from its universality. Any computer can
|
---|
44 | read a text file. With the proper tools, any computer can read any other
|
---|
45 | computer's <acronym>XML</acronym> files.
|
---|
46 | </para>
|
---|
47 |
|
---|
48 | <para>One of the most important of those tools is <acronym>XSLT</acronym>:
|
---|
49 | Extensible Stylesheet Language Transformations. <acronym>XSLT</acronym>
|
---|
50 | is a declarative language that allows you to
|
---|
51 | translate your <acronym>XML</acronym> into arbitrary text output
|
---|
52 | using a stylesheet. <application>libxslt</application> provides the
|
---|
53 | functions to perform the transformation.
|
---|
54 | </para>
|
---|
55 |
|
---|
56 | <para><application>libxslt</application> is a free C language library
|
---|
57 | written by Daniel Veillard for the <acronym>GNOME</acronym> project
|
---|
58 | allowing you to write programs that perform <acronym>XSLT</acronym>
|
---|
59 | transformations.
|
---|
60 |
|
---|
61 | <note>
|
---|
62 | <para>
|
---|
63 | While <application>libxslt</application> was written
|
---|
64 | under the auspices of the <acronym>GNOME</acronym> project, it does not
|
---|
65 | depend on any <acronym>GNOME</acronym> libraries. None are used in the
|
---|
66 | example in this tutorial.
|
---|
67 | </para>
|
---|
68 | </note>
|
---|
69 |
|
---|
70 | </para>
|
---|
71 |
|
---|
72 | <para>This tutorial illustrates a simple program that reads an
|
---|
73 | <acronym>XML</acronym> file, applies a stylesheet and saves the resulting
|
---|
74 | output. This is not a program you would want to create
|
---|
75 | yourself. <application>xsltproc</application>, which is included with the
|
---|
76 | <application>libxslt</application> package, does the same thing and is
|
---|
77 | more robust and full-featured. The program written for this tutorial is a
|
---|
78 | stripped-down version of <application>xsltproc</application> designed to
|
---|
79 | illustrate the functionality of <application>libxslt</application>.
|
---|
80 | </para>
|
---|
81 | <para>The full code for <application>xsltproc</application> is in
|
---|
82 | <filename>xsltproc.c</filename> in the <application>libxslt</application>
|
---|
83 | distribution. It also is available <ulink
|
---|
84 | url="http://cvs.gnome.org/lxr/source/libxslt/libxslt/xsltproc.c">on the
|
---|
85 | web</ulink>.
|
---|
86 | </para>
|
---|
87 |
|
---|
88 | <para>References:
|
---|
89 | <itemizedlist>
|
---|
90 | <listitem>
|
---|
91 | <para><ulink url="http://www.w3.org/XML/">W3C <acronym>XML</acronym> page</ulink></para>
|
---|
92 | </listitem>
|
---|
93 | <listitem>
|
---|
94 | <para><ulink url="http://www.w3.org/Style/XSL/">W3C
|
---|
95 | <acronym>XSL</acronym> page.</ulink></para>
|
---|
96 | </listitem>
|
---|
97 | <listitem>
|
---|
98 | <para><ulink url="http://xmlsoft.org/XSLT/">libxslt</ulink></para>
|
---|
99 | </listitem>
|
---|
100 | </itemizedlist>
|
---|
101 |
|
---|
102 | </para>
|
---|
103 | </sect1>
|
---|
104 |
|
---|
105 | <sect1 id="functions">
|
---|
106 | <title>Primary Functions</title>
|
---|
107 | <para>To transform an <acronym>XML</acronym> file, you must perform three
|
---|
108 | functions:
|
---|
109 | <orderedlist>
|
---|
110 | <listitem>
|
---|
111 | <para>parse the input file</para>
|
---|
112 | </listitem>
|
---|
113 | <listitem>
|
---|
114 | <para>parse the stylesheet</para>
|
---|
115 | </listitem>
|
---|
116 | <listitem>
|
---|
117 | <para>apply the stylesheet</para>
|
---|
118 | </listitem>
|
---|
119 | </orderedlist>
|
---|
120 | </para>
|
---|
121 | <sect2 id="preparing">
|
---|
122 | <title>Preparing to Parse</title>
|
---|
123 | <para>Before you can begin parsing input files or stylesheets, there are
|
---|
124 | several steps you need to take to set up entity handling. These steps are
|
---|
125 | not unique to <application>libxslt</application>. Any
|
---|
126 | <application>libxml2</application> program that parses
|
---|
127 | <acronym>XML</acronym> files would need to take similar steps.
|
---|
128 | </para>
|
---|
129 | <para>First, you need set up some <application>libxml</application>
|
---|
130 | housekeeping. Pass the integer value <parameter>1</parameter> to the
|
---|
131 | <function>xmlSubstituteEntitiesDefault</function> function, which tells
|
---|
132 | the <application>libxml2</application> parser to substitute entities as
|
---|
133 | it parses your file. (Passing <parameter>0</parameter> causes
|
---|
134 | <application>libxml2</application> to not perform entity substitution.)
|
---|
135 | </para>
|
---|
136 |
|
---|
137 | <para>Second, set <varname>xmlLoadExtDtdDefaultValue</varname> equal to
|
---|
138 | <parameter>1</parameter>. This tells <application>libxml</application>
|
---|
139 | to load external entity subsets. If you do not do this and your
|
---|
140 | input file includes entities through external subsets, you will get
|
---|
141 | errors.</para>
|
---|
142 | </sect2>
|
---|
143 | <sect2 id="parsethestylesheet">
|
---|
144 | <title>Parse the Stylesheet</title>
|
---|
145 | <para>Parsing the stylesheet takes a single function call, which takes a
|
---|
146 | variable of type <type>xmlChar</type>:
|
---|
147 | <programlisting>
|
---|
148 | <varname>cur</varname> = xsltParseStylesheetFile((const xmlChar *)argv[i]);
|
---|
149 | </programlisting>
|
---|
150 | In this case, I cast the stylesheet file name, passed in as a
|
---|
151 | command line argument, to <emphasis>xmlChar</emphasis>. The return value
|
---|
152 | is of type <emphasis>xsltStylesheetPtr</emphasis>, a struct in memory
|
---|
153 | that contains the stylesheet tree and other information about the
|
---|
154 | stylesheet. It can be manipulated directly, but for this example you
|
---|
155 | will not need to.
|
---|
156 | </para>
|
---|
157 | </sect2>
|
---|
158 |
|
---|
159 | <sect2 id="parseinputfile">
|
---|
160 | <title>Parse the Input File</title>
|
---|
161 | <para>Parsing the input file takes a single function call:
|
---|
162 | <programlisting>
|
---|
163 | doc = xmlParseFile(argv[i]);
|
---|
164 | </programlisting>
|
---|
165 | It returns an <emphasis>xmlDocPtr</emphasis>, a struct in memory that
|
---|
166 | contains the document tree. It can be manipulated directly, but for this
|
---|
167 | example you will not need to.
|
---|
168 | </para>
|
---|
169 | </sect2>
|
---|
170 |
|
---|
171 | <sect2 id="applyingstylesheet">
|
---|
172 | <title>Applying the Stylesheet</title>
|
---|
173 | <para>Now that you have trees representing the document and the stylesheet
|
---|
174 | in memory, apply the stylesheet to the document. The
|
---|
175 | function that does this is <function>xsltApplyStylesheet</function>:
|
---|
176 | <programlisting>
|
---|
177 | res = xsltApplyStylesheet(cur, doc, params);
|
---|
178 | </programlisting>
|
---|
179 | The function takes an xsltStylesheetPtr and an
|
---|
180 | xmlDocPtr, the values returned by the previous two functions. The third
|
---|
181 | variable, <varname>params</varname> can be used to pass
|
---|
182 | <acronym>XSLT</acronym> parameters to the stylesheet. It is a
|
---|
183 | NULL-terminated array of name/value pairs of const char's.
|
---|
184 | </para>
|
---|
185 | </sect2>
|
---|
186 |
|
---|
187 | <sect2 id="saveresult">
|
---|
188 | <title>Saving the result</title>
|
---|
189 | <para><application>libxslt</application> includes a family of functions to use in
|
---|
190 | saving the resulting output. For this example,
|
---|
191 | <function>xsltSaveResultToFile</function> is used, and the results are
|
---|
192 | saved to stdout:
|
---|
193 |
|
---|
194 | <programlisting>
|
---|
195 | xsltSaveResultToFile(stdout, res, cur);
|
---|
196 | </programlisting>
|
---|
197 |
|
---|
198 | <note>
|
---|
199 | <para><application>libxml</application> also contains output
|
---|
200 | functions, such as <function>xmlSaveFile</function>, which can be
|
---|
201 | used here. However, output-related information contained in the
|
---|
202 | stylesheet, such as a declaration of the encoding to be used, will
|
---|
203 | be lost if one of the <application>libxslt</application> save
|
---|
204 | functions is not used.</para>
|
---|
205 | </note>
|
---|
206 | </para>
|
---|
207 | </sect2>
|
---|
208 |
|
---|
209 | <sect2 id="parameters">
|
---|
210 | <title>Parameters</title>
|
---|
211 | <para>
|
---|
212 | In <acronym>XSLT</acronym>, parameters may be used as a way to pass
|
---|
213 | additional information to a
|
---|
214 | stylesheet. <application>libxslt</application> accepts
|
---|
215 | <acronym>XSLT</acronym> parameters as one of the values passed to
|
---|
216 | <function>xsltApplyStylesheet</function>.
|
---|
217 | </para>
|
---|
218 |
|
---|
219 | <para>
|
---|
220 | In the tutorial example and in <application>xsltproc</application>,
|
---|
221 | on which the tutorial example is based, parameters to be passed take the
|
---|
222 | form of key-value pairs. The program collects them from command line
|
---|
223 | arguments, inserting them in the array <varname>params</varname>, then
|
---|
224 | passes them to the function. The final element in the array is set to
|
---|
225 | <parameter>NULL</parameter>.
|
---|
226 |
|
---|
227 | <note>
|
---|
228 | <para>
|
---|
229 | If a parameter being passed is a string rather than an
|
---|
230 | <acronym>XSLT</acronym> node, it must be escaped. For the tutorial
|
---|
231 | program, that would be done as follows:
|
---|
232 | <command>tutorial]$ ./libxslt_tutorial --param rootid "'asect1'"
|
---|
233 | stylesheet.xsl filename.xml</command>
|
---|
234 | </para>
|
---|
235 | </note>
|
---|
236 | </para>
|
---|
237 |
|
---|
238 | </sect2>
|
---|
239 |
|
---|
240 | <sect2 id="cleanup">
|
---|
241 | <title>Cleanup</title>
|
---|
242 | <para>After you are finished, <application>libxslt</application> and
|
---|
243 | <application>libxml</application> provide functions for deallocating
|
---|
244 | memory.
|
---|
245 | </para>
|
---|
246 |
|
---|
247 | <para>
|
---|
248 |
|
---|
249 | <programlisting>
|
---|
250 | xsltFreeStylesheet(cur);<co id="cleanupstylesheet" />
|
---|
251 | xmlFreeDoc(res);<co id="cleanupresults" />
|
---|
252 | xmlFreeDoc(doc);<co id="cleanupdoc" />
|
---|
253 | xsltCleanupGlobals();<co id="cleanupglobals" />
|
---|
254 | xmlCleanupParser();<co id="cleanupparser" />
|
---|
255 |
|
---|
256 | </programlisting>
|
---|
257 |
|
---|
258 | <calloutlist>
|
---|
259 | <callout arearefs="cleanupstylesheet">
|
---|
260 | <para>Free the memory used by your stylesheet.</para>
|
---|
261 | </callout>
|
---|
262 | <callout arearefs="cleanupresults">
|
---|
263 | <para>Free the memory used by the results document.</para>
|
---|
264 | </callout>
|
---|
265 | <callout arearefs="cleanupdoc">
|
---|
266 | <para>Free the memory used by your original document.</para>
|
---|
267 | </callout>
|
---|
268 | <callout arearefs="cleanupglobals">
|
---|
269 | <para>Free memory used by <application>libxslt</application> global
|
---|
270 | variables</para>
|
---|
271 | </callout>
|
---|
272 | <callout arearefs="cleanupparser">
|
---|
273 | <para>Free memory used by the <acronym>XML</acronym> parser</para>
|
---|
274 | </callout>
|
---|
275 | </calloutlist>
|
---|
276 | </para>
|
---|
277 | </sect2>
|
---|
278 |
|
---|
279 | </sect1>
|
---|
280 |
|
---|
281 | <appendix id="thecode">
|
---|
282 | <title>The Code</title>
|
---|
283 | <para><filename>libxslt_tutorial.c</filename>
|
---|
284 | <programlisting>&CODE;</programlisting>
|
---|
285 |
|
---|
286 | </para>
|
---|
287 | </appendix>
|
---|
288 | </article>
|
---|