reference/xsl/examples.xml
10b60deaa2e4353299a66e99eae0f06d53ddb661
...
...
@@ -4,6 +4,7 @@
4
4
<chapter xml:id="xsl.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
5
5
&reftitle.examples;
6
6
<section xml:id="xsl.examples-collection">
7
+
<title>Example collection.xml and collection.xsl files</title>
7
8
<para>
8
9
Many examples in this reference require both an XML and an XSL file.
9
10
We will use <filename>collection.xml</filename> and
...
...
@@ -51,6 +52,93 @@
51
52
</example>
52
53
</para>
53
54
</section>
55
+

56
+
<section xml:id="xsl.examples-errors">
57
+
<title>Error handling with libxml error handling functions</title>
58
+
<para>
59
+
libxml offers a number of functions for handling errors, which can be
60
+
employed to capture and deal with errors in XSLT processing.
61
+
</para>
62
+
<para>
63
+
<example>
64
+
<title>fruits.xml</title>
65
+
<para>A valid XML file.</para>
66
+
<programlisting role="xml">
67
+
<![CDATA[
68
+
<fruits>
69
+
<fruit>Apple</fruit>
70
+
<fruit>Banana</fruit>
71
+
<fruit>Cherry</fruit>
72
+
</fruits>
73
+
]]>
74
+
</programlisting>
75
+
</example>
76
+
<example>
77
+
<title>fruits.xsl</title>
78
+
<para>Contains an invalid select expression.</para>
79
+
<programlisting role="xml">
80
+
<![CDATA[
81
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
82
+
<xsl:output method="html" encoding="utf-8" indent="no"/>
83
+
<xsl:template match="fruits">
84
+
<ul>
85
+
<xsl:apply-templates/>
86
+
</ul>
87
+
</xsl:template>
88
+
<xsl:template match="fruit">
89
+
<li><xsl:value-of select="THIS IS A DELIBERATE ERROR!"/></li>
90
+
</xsl:template>
91
+
</xsl:stylesheet>
92
+
]]>
93
+
</programlisting>
94
+
</example>
95
+
<example xml:id="xsl.examples-errors.capture">
96
+
<title>Collating and printing errors</title>
97
+
<para>
98
+
The example below captures and displays libxml errors raised when calling
99
+
<methodname>XSLTProcessor::importStyleSheet</methodname> with a
100
+
stylesheet containing an error.
101
+
</para>
102
+
<programlisting role="php">
103
+
<![CDATA[
104
+
<?php
105
+

106
+
$xmldoc = new DOMDocument();
107
+
$xsldoc = new DOMDocument();
108
+
$xsl = new XSLTProcessor();
109
+

110
+
$xmldoc->loadXML('fruits.xml');
111
+
$xsldoc->loadXML('fruits.xsl');
112
+

113
+
libxml_use_internal_errors(true);
114
+
$result = $xsl->importStyleSheet($xsldoc);
115
+
if (!$result) {
116
+
foreach (libxml_get_errors() as $error) {
117
+
echo "Libxml error: {$error->message}\n";
118
+
}
119
+
}
120
+
libxml_use_internal_errors(false);
121
+

122
+
if ($result) {
123
+
echo $xsl->transformToXML($xmldoc);
124
+
}
125
+

126
+
?>
127
+
]]>
128
+
</programlisting>
129
+
&example.outputs.similar;
130
+
<screen>
131
+
<![CDATA[
132
+
Libxml error: Invalid expression
133
+

134
+
Libxml error: compilation error: file fruits.xsl line 9 element value-of
135
+
Libxml error: xsl:value-of : could not compile select expression 'THIS IS A DELIBERATE ERROR!'
136
+
]]>
137
+
</screen>
138
+
</example>
139
+
</para>
140
+
</section>
141
+

54
142
</chapter>
55
143
<!-- Keep this comment at the end of the file
56
144
Local variables:
57
145