| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* Examples (file #1)
|
|
|
7 |
*
|
|
|
8 |
* several examples for the methods of XML_Util
|
|
|
9 |
*
|
|
|
10 |
* PHP versions 4 and 5
|
|
|
11 |
*
|
|
|
12 |
* LICENSE:
|
|
|
13 |
*
|
|
|
14 |
* Copyright (c) 2003-2008 Stephan Schmidt <schst@php.net>
|
|
|
15 |
* All rights reserved.
|
|
|
16 |
*
|
|
|
17 |
* Redistribution and use in source and binary forms, with or without
|
|
|
18 |
* modification, are permitted provided that the following conditions
|
|
|
19 |
* are met:
|
|
|
20 |
*
|
|
|
21 |
* * Redistributions of source code must retain the above copyright
|
|
|
22 |
* notice, this list of conditions and the following disclaimer.
|
|
|
23 |
* * Redistributions in binary form must reproduce the above copyright
|
|
|
24 |
* notice, this list of conditions and the following disclaimer in the
|
|
|
25 |
* documentation and/or other materials provided with the distribution.
|
|
|
26 |
* * The name of the author may not be used to endorse or promote products
|
|
|
27 |
* derived from this software without specific prior written permission.
|
|
|
28 |
*
|
|
|
29 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
|
30 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
31 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
32 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
33 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
34 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
35 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
36 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
|
37 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
38 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
39 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
40 |
*
|
|
|
41 |
* @category XML
|
|
|
42 |
* @package XML_Util
|
|
|
43 |
* @subpackage Examples
|
|
|
44 |
* @author Stephan Schmidt <schst@php.net>
|
|
|
45 |
* @copyright 2003-2008 Stephan Schmidt <schst@php.net>
|
|
|
46 |
* @license http://opensource.org/licenses/bsd-license New BSD License
|
|
|
47 |
* @version CVS: $Id: example.php,v 1.17 2008/05/05 19:05:28 ashnazg Exp $
|
|
|
48 |
* @link http://pear.php.net/package/XML_Util
|
|
|
49 |
*/
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* set error level
|
|
|
53 |
*/
|
|
|
54 |
error_reporting(E_ALL);
|
|
|
55 |
|
|
|
56 |
require_once 'XML/Util.php';
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* replacing XML entities
|
|
|
60 |
*/
|
|
|
61 |
print 'replace XML entities:<br>';
|
|
|
62 |
print XML_Util::replaceEntities('This string contains < & >.');
|
|
|
63 |
print "\n<br><br>\n";
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* reversing XML entities
|
|
|
67 |
*/
|
|
|
68 |
print 'replace XML entities:<br>';
|
|
|
69 |
print XML_Util::reverseEntities('This string contains < & >.');
|
|
|
70 |
print "\n<br><br>\n";
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* building XML declaration
|
|
|
74 |
*/
|
|
|
75 |
print 'building XML declaration:<br>';
|
|
|
76 |
print htmlspecialchars(XML_Util::getXMLDeclaration());
|
|
|
77 |
print "\n<br><br>\n";
|
|
|
78 |
|
|
|
79 |
print 'building XML declaration with additional attributes:<br>';
|
|
|
80 |
print htmlspecialchars(XML_Util::getXMLDeclaration('1.0', 'UTF-8', true));
|
|
|
81 |
print "\n<br><br>\n";
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* building document type declaration
|
|
|
85 |
*/
|
|
|
86 |
print 'building DocType declaration:<br>';
|
|
|
87 |
print htmlspecialchars(XML_Util::getDocTypeDeclaration('package',
|
|
|
88 |
'http://pear.php.net/dtd/package-1.0'));
|
|
|
89 |
print "\n<br><br>\n";
|
|
|
90 |
|
|
|
91 |
print 'building DocType declaration with public ID (does not exist):<br>';
|
|
|
92 |
print htmlspecialchars(XML_Util::getDocTypeDeclaration('package',
|
|
|
93 |
array('uri' => 'http://pear.php.net/dtd/package-1.0',
|
|
|
94 |
'id' => '-//PHP//PEAR/DTD PACKAGE 0.1')));
|
|
|
95 |
print "\n<br><br>\n";
|
|
|
96 |
|
|
|
97 |
print 'building DocType declaration with internal DTD:<br>';
|
|
|
98 |
print '<pre>';
|
|
|
99 |
print htmlspecialchars(XML_Util::getDocTypeDeclaration('package',
|
|
|
100 |
'http://pear.php.net/dtd/package-1.0',
|
|
|
101 |
'<!ELEMENT additionalInfo (#PCDATA)>'));
|
|
|
102 |
print '</pre>';
|
|
|
103 |
print "\n<br><br>\n";
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* creating an attribute string
|
|
|
107 |
*/
|
|
|
108 |
$att = array(
|
|
|
109 |
'foo' => 'bar',
|
|
|
110 |
'argh' => 'tomato'
|
|
|
111 |
);
|
|
|
112 |
|
|
|
113 |
print 'converting array to string:<br>';
|
|
|
114 |
print XML_Util::attributesToString($att);
|
|
|
115 |
print "\n<br><br>\n";
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* creating an attribute string with linebreaks
|
|
|
120 |
*/
|
|
|
121 |
$att = array(
|
|
|
122 |
'foo' => 'bar',
|
|
|
123 |
'argh' => 'tomato'
|
|
|
124 |
);
|
|
|
125 |
|
|
|
126 |
print 'converting array to string (including line breaks):<br>';
|
|
|
127 |
print '<pre>';
|
|
|
128 |
print XML_Util::attributesToString($att, true, true);
|
|
|
129 |
print '</pre>';
|
|
|
130 |
print "\n<br><br>\n";
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* splitting a qualified tag name
|
|
|
135 |
*/
|
|
|
136 |
print 'splitting qualified tag name:<br>';
|
|
|
137 |
print '<pre>';
|
|
|
138 |
print_r(XML_Util::splitQualifiedName('xslt:stylesheet'));
|
|
|
139 |
print '</pre>';
|
|
|
140 |
print "\n<br>\n";
|
|
|
141 |
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* splitting a qualified tag name (no namespace)
|
|
|
145 |
*/
|
|
|
146 |
print 'splitting qualified tag name (no namespace):<br>';
|
|
|
147 |
print '<pre>';
|
|
|
148 |
print_r(XML_Util::splitQualifiedName('foo'));
|
|
|
149 |
print '</pre>';
|
|
|
150 |
print "\n<br>\n";
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* splitting a qualified tag name (no namespace, but default namespace specified)
|
|
|
154 |
*/
|
|
|
155 |
print 'splitting qualified tag name '
|
|
|
156 |
. '(no namespace, but default namespace specified):<br>';
|
|
|
157 |
print '<pre>';
|
|
|
158 |
print_r(XML_Util::splitQualifiedName('foo', 'bar'));
|
|
|
159 |
print '</pre>';
|
|
|
160 |
print "\n<br>\n";
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* verifying XML names
|
|
|
164 |
*/
|
|
|
165 |
print 'verifying \'My private tag\':<br>';
|
|
|
166 |
print '<pre>';
|
|
|
167 |
print_r(XML_Util::isValidname('My Private Tag'));
|
|
|
168 |
print '</pre>';
|
|
|
169 |
print "\n<br><br>\n";
|
|
|
170 |
|
|
|
171 |
print 'verifying \'-MyTag\':<br>';
|
|
|
172 |
print '<pre>';
|
|
|
173 |
print_r(XML_Util::isValidname('-MyTag'));
|
|
|
174 |
print '</pre>';
|
|
|
175 |
print "\n<br><br>\n";
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* creating an XML tag
|
|
|
179 |
*/
|
|
|
180 |
$tag = array(
|
|
|
181 |
'namespace' => 'foo',
|
|
|
182 |
'localPart' => 'bar',
|
|
|
183 |
'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
|
|
|
184 |
'content' => 'I\'m inside the tag'
|
|
|
185 |
);
|
|
|
186 |
|
|
|
187 |
print 'creating a tag with namespace and local part:<br>';
|
|
|
188 |
print htmlentities(XML_Util::createTagFromArray($tag));
|
|
|
189 |
print "\n<br><br>\n";
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* creating an XML tag
|
|
|
193 |
*/
|
|
|
194 |
$tag = array(
|
|
|
195 |
'qname' => 'foo:bar',
|
|
|
196 |
'namespaceUri' => 'http://foo.com',
|
|
|
197 |
'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
|
|
|
198 |
'content' => 'I\'m inside the tag'
|
|
|
199 |
);
|
|
|
200 |
|
|
|
201 |
print 'creating a tag with qualified name and namespaceUri:<br>';
|
|
|
202 |
print htmlentities(XML_Util::createTagFromArray($tag));
|
|
|
203 |
print "\n<br><br>\n";
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* creating an XML tag
|
|
|
207 |
*/
|
|
|
208 |
$tag = array(
|
|
|
209 |
'qname' => 'bar',
|
|
|
210 |
'namespaceUri' => 'http://foo.com',
|
|
|
211 |
'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable')
|
|
|
212 |
);
|
|
|
213 |
|
|
|
214 |
print 'creating an empty tag without namespace but namespace Uri:<br>';
|
|
|
215 |
print htmlentities(XML_Util::createTagFromArray($tag));
|
|
|
216 |
print "\n<br><br>\n";
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* creating an XML tag with more namespaces
|
|
|
220 |
*/
|
|
|
221 |
$tag = array(
|
|
|
222 |
'namespace' => 'foo',
|
|
|
223 |
'localPart' => 'bar',
|
|
|
224 |
'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
|
|
|
225 |
'content' => 'I\'m inside the tag',
|
|
|
226 |
'namespaces' => array(
|
|
|
227 |
'bar' => 'http://bar.com',
|
|
|
228 |
'pear' => 'http://pear.php.net',
|
|
|
229 |
)
|
|
|
230 |
);
|
|
|
231 |
|
|
|
232 |
print 'creating an XML tag with more namespaces:<br />';
|
|
|
233 |
print htmlentities(XML_Util::createTagFromArray($tag));
|
|
|
234 |
print "\n<br><br>\n";
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* creating an XML tag with a CData Section
|
|
|
238 |
*/
|
|
|
239 |
$tag = array(
|
|
|
240 |
'qname' => 'foo',
|
|
|
241 |
'attributes' => array('key' => 'value', 'argh' => 'fruit&vegetable'),
|
|
|
242 |
'content' => 'I\'m inside the tag'
|
|
|
243 |
);
|
|
|
244 |
|
|
|
245 |
print 'creating a tag with CData section:<br>';
|
|
|
246 |
print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_CDATA_SECTION));
|
|
|
247 |
print "\n<br><br>\n";
|
|
|
248 |
|
|
|
249 |
/**
|
|
|
250 |
* creating an XML tag with a CData Section
|
|
|
251 |
*/
|
|
|
252 |
$tag = array(
|
|
|
253 |
'qname' => 'foo',
|
|
|
254 |
'attributes' => array('key' => 'value', 'argh' => 'tütü'),
|
|
|
255 |
'content' =>
|
|
|
256 |
'Also XHTML-tags can be created '
|
|
|
257 |
. 'and HTML entities can be replaced Ä ä Ü ö <>.'
|
|
|
258 |
);
|
|
|
259 |
|
|
|
260 |
print 'creating a tag with HTML entities:<br>';
|
|
|
261 |
print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_ENTITIES_HTML));
|
|
|
262 |
print "\n<br><br>\n";
|
|
|
263 |
|
|
|
264 |
/**
|
|
|
265 |
* creating an XML tag with createTag
|
|
|
266 |
*/
|
|
|
267 |
print 'creating a tag with createTag:<br>';
|
|
|
268 |
print htmlentities(XML_Util::createTag('myNs:myTag',
|
|
|
269 |
array('foo' => 'bar'),
|
|
|
270 |
'This is inside the tag',
|
|
|
271 |
'http://www.w3c.org/myNs#'));
|
|
|
272 |
print "\n<br><br>\n";
|
|
|
273 |
|
|
|
274 |
|
|
|
275 |
/**
|
|
|
276 |
* trying to create an XML tag with an array as content
|
|
|
277 |
*/
|
|
|
278 |
$tag = array(
|
|
|
279 |
'qname' => 'bar',
|
|
|
280 |
'content' => array('foo' => 'bar')
|
|
|
281 |
);
|
|
|
282 |
print 'trying to create an XML tag with an array as content:<br>';
|
|
|
283 |
print '<pre>';
|
|
|
284 |
print_r(XML_Util::createTagFromArray($tag));
|
|
|
285 |
print '</pre>';
|
|
|
286 |
print "\n<br><br>\n";
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* trying to create an XML tag without a name
|
|
|
290 |
*/
|
|
|
291 |
$tag = array(
|
|
|
292 |
'attributes' => array('foo' => 'bar'),
|
|
|
293 |
);
|
|
|
294 |
print 'trying to create an XML tag without a name:<br>';
|
|
|
295 |
print '<pre>';
|
|
|
296 |
print_r(XML_Util::createTagFromArray($tag));
|
|
|
297 |
print '</pre>';
|
|
|
298 |
print "\n<br><br>\n";
|
|
|
299 |
?>
|