/include/xml/examples/example.php

https://github.com/prometheus-ev/promdilps · PHP · 228 lines · 154 code · 25 blank · 49 comment · 0 complexity · 09e113e95845ceac6aed018d6013d166 MD5 · raw file

  1. <?PHP
  2. /**
  3. * several examples for the methods of XML_Util
  4. *
  5. * $Id: example.php,v 1.1.1.1 2006/01/11 01:40:06 sdoeweling Exp $
  6. *
  7. * @author Stephan Schmidt
  8. * @package XML_Util
  9. * @subpackage examples
  10. * @category XML
  11. */
  12. error_reporting(E_ALL);
  13. require_once 'XML/Util.php';
  14. /**
  15. * replacing XML entities
  16. */
  17. print "replace XML entities:<br>\n";
  18. print XML_Util::replaceEntities("This string contains < & >.");
  19. print "\n<br><br>\n";
  20. /**
  21. * reversing XML entities
  22. */
  23. print "replace XML entities:<br>\n";
  24. print XML_Util::reverseEntities("This string contains &lt; &amp; &gt;.");
  25. print "\n<br><br>\n";
  26. /**
  27. * building XML declaration
  28. */
  29. print "building XML declaration:<br>\n";
  30. print htmlspecialchars(XML_Util::getXMLDeclaration());
  31. print "\n<br><br>\n";
  32. print "building XML declaration with additional attributes:<br>";
  33. print htmlspecialchars(XML_Util::getXMLDeclaration("1.0", "UTF-8", true));
  34. print "\n<br><br>\n";
  35. /**
  36. * building document type declaration
  37. */
  38. print "building DocType declaration:<br>\n";
  39. print htmlspecialchars(XML_Util::getDocTypeDeclaration('package', 'http://pear.php.net/dtd/package-1.0'));
  40. print "\n<br><br>\n";
  41. print "building DocType declaration with public ID (does not exist):<br>\n";
  42. print htmlspecialchars(XML_Util::getDocTypeDeclaration('package', array('uri' => 'http://pear.php.net/dtd/package-1.0', 'id' => '-//PHP//PEAR/DTD PACKAGE 0.1')));
  43. print "\n<br><br>\n";
  44. print "building DocType declaration with internal DTD:<br>\n";
  45. print "<pre>";
  46. print htmlspecialchars(XML_Util::getDocTypeDeclaration('package', 'http://pear.php.net/dtd/package-1.0', '<!ELEMENT additionalInfo (#PCDATA)>'));
  47. print "</pre>";
  48. print "\n<br><br>\n";
  49. /**
  50. * creating an attribute string
  51. */
  52. $att = array(
  53. "foo" => "bar",
  54. "argh" => "tomato"
  55. );
  56. print "converting array to string:<br>\n";
  57. print XML_Util::attributesToString($att);
  58. print "\n<br><br>\n";
  59. /**
  60. * creating an attribute string with linebreaks
  61. */
  62. $att = array(
  63. "foo" => "bar",
  64. "argh" => "tomato"
  65. );
  66. print "converting array to string (including line breaks):<br>\n";
  67. print "<pre>";
  68. print XML_Util::attributesToString($att, true, true);
  69. print "</pre>";
  70. print "\n<br><br>\n";
  71. /**
  72. * splitting a qualified tag name
  73. */
  74. print "splitting qualified tag name:<br>\n";
  75. print "<pre>";
  76. print_r(XML_Util::splitQualifiedName("xslt:stylesheet"));
  77. print "</pre>";
  78. print "\n<br>\n";
  79. /**
  80. * splitting a qualified tag name (no namespace)
  81. */
  82. print "splitting qualified tag name (no namespace):<br>\n";
  83. print "<pre>";
  84. print_r(XML_Util::splitQualifiedName("foo"));
  85. print "</pre>";
  86. print "\n<br>\n";
  87. /**
  88. * splitting a qualified tag name (no namespace, but default namespace specified)
  89. */
  90. print "splitting qualified tag name (no namespace, but default namespace specified):<br>\n";
  91. print "<pre>";
  92. print_r(XML_Util::splitQualifiedName("foo", "bar"));
  93. print "</pre>";
  94. print "\n<br>\n";
  95. /**
  96. * verifying XML names
  97. */
  98. print "verifying 'My private tag':<br>\n";
  99. print "<pre>";
  100. print_r(XML_Util::isValidname('My Private Tag'));
  101. print "</pre>";
  102. print "\n<br><br>\n";
  103. print "verifying '-MyTag':<br>\n";
  104. print "<pre>";
  105. print_r(XML_Util::isValidname('-MyTag'));
  106. print "</pre>";
  107. print "\n<br><br>\n";
  108. /**
  109. * creating an XML tag
  110. */
  111. $tag = array(
  112. "namespace" => "foo",
  113. "localPart" => "bar",
  114. "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ),
  115. "content" => "I'm inside the tag"
  116. );
  117. print "creating a tag with namespace and local part:<br>";
  118. print htmlentities(XML_Util::createTagFromArray($tag));
  119. print "\n<br><br>\n";
  120. /**
  121. * creating an XML tag
  122. */
  123. $tag = array(
  124. "qname" => "foo:bar",
  125. "namespaceUri" => "http://foo.com",
  126. "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ),
  127. "content" => "I'm inside the tag"
  128. );
  129. print "creating a tag with qualified name and namespaceUri:<br>\n";
  130. print htmlentities(XML_Util::createTagFromArray($tag));
  131. print "\n<br><br>\n";
  132. /**
  133. * creating an XML tag
  134. */
  135. $tag = array(
  136. "qname" => "bar",
  137. "namespaceUri" => "http://foo.com",
  138. "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" )
  139. );
  140. print "creating an empty tag without namespace but namespace Uri:<br>\n";
  141. print htmlentities(XML_Util::createTagFromArray($tag));
  142. print "\n<br><br>\n";
  143. /**
  144. * creating an XML tag with a CData Section
  145. */
  146. $tag = array(
  147. "qname" => "foo",
  148. "attributes" => array( "key" => "value", "argh" => "fruit&vegetable" ),
  149. "content" => "I'm inside the tag"
  150. );
  151. print "creating a tag with CData section:<br>\n";
  152. print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_CDATA_SECTION));
  153. print "\n<br><br>\n";
  154. /**
  155. * creating an XML tag with a CData Section
  156. */
  157. $tag = array(
  158. "qname" => "foo",
  159. "attributes" => array( "key" => "value", "argh" => "tütü" ),
  160. "content" => "Also XHTML-tags can be created and HTML entities can be replaced Ä ä Ü ö <>."
  161. );
  162. print "creating a tag with HTML entities:<br>\n";
  163. print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_ENTITIES_HTML));
  164. print "\n<br><br>\n";
  165. /**
  166. * creating an XML tag with createTag
  167. */
  168. print "creating a tag with createTag:<br>";
  169. print htmlentities(XML_Util::createTag("myNs:myTag", array("foo" => "bar"), "This is inside the tag", "http://www.w3c.org/myNs#"));
  170. print "\n<br><br>\n";
  171. /**
  172. * trying to create an XML tag with an array as content
  173. */
  174. $tag = array(
  175. "qname" => "bar",
  176. "content" => array( "foo" => "bar" )
  177. );
  178. print "trying to create an XML tag with an array as content:<br>\n";
  179. print "<pre>";
  180. print_r(XML_Util::createTagFromArray($tag));
  181. print "</pre>";
  182. print "\n<br><br>\n";
  183. /**
  184. * trying to create an XML tag without a name
  185. */
  186. $tag = array(
  187. "attributes" => array( "foo" => "bar" ),
  188. );
  189. print "trying to create an XML tag without a name:<br>\n";
  190. print "<pre>";
  191. print_r(XML_Util::createTagFromArray($tag));
  192. print "</pre>";
  193. print "\n<br><br>\n";
  194. ?>