summaryrefslogtreecommitdiff
path: root/tests/testdom
blob: d12961f8c9f854bbf06631109690d90c1b2361f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

function output_node($node, $level=0) {
  switch($node->type) {
    case XML_ELEMENT_NODE:
      for($i=0; $i<$level; $i++)
        echo "  ";
      echo "<".$node->name;
      $attributes = $node->attributes();
      if(is_array($attributes)) {
//var_dump($attributes);
        foreach($attributes as $attribute)
          echo " ".$attribute->name."=".$node->getattr($attribute->name);
      }
      echo ">\n";
      $children = $node->children();
      for($i=0; $i < count($children); $i++)
        output_node($children[$i], $level+1);
      for($i=0; $i<$level; $i++)
        echo "  ";
      echo "</".$node->name.">\n";
      break;
    case XML_TEXT_NODE:
      for($i=0; $i<$level; $i++)
        echo "  ";
      echo $node->content;
      break;
    case XML_ENTITY_REF_NODE:
      echo $node->content;
      break;
    case XML_COMMENT_NODE:
      for($i=0; $i<$level; $i++)
        echo "  ";
      echo "<!--".$node->content."-->";
      echo "\n";
      break;
  }
}

function list_attr($node) {
  $attr = domxml_attributes($node);
  for(reset($attr); $key = key($attr); next($attr)) {
    echo $key."=".$attr[$key]."\n";
  }
}

$xmlstr = "<?xml version='1.0' standalone='yes'?>
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
[ <!ENTITY sp \"spanish\">
]>
<!-- lsfj  -->
<chapter language='en'><title language='en'>Title</title>
<para language='ge'>
&sp;
<!-- comment -->
<informaltable language='&sp;kkk'>
<tgroup cols='3'>
<tbody>
<row><entry>a1</entry><entry morerows='1'>b1</entry><entry>c1</entry></row>
<row><entry>a2</entry><entry>c2</entry></row>
<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
</tbody>
</tgroup>
</informaltable>
</para>
</chapter> ";

/* The following code traverses the xml tree node by node
   by using the methods of the xmlnode object
*/
echo "Test 1: accessing single nodes from php\n";
if(!$dom = xmldoc($xmlstr)) {
  echo "Error while parsing the document\n";
  exit;
}
echo "XML Version: ".$dom->version."\n";
echo "Standalone: ".$dom->standalone."\n";
$dtd = $dom->dtd();
$rootnode = $dom->children();
foreach($rootnode as $root)
  output_node($root);
$rootnode = $dom->root();
  output_node($rootnode);

/* This one creates a dom tree made of php objects */
echo "Test 2: creating a tree with php objects\n";
$dom = xmltree($xmlstr);
$dom->root->name = "section";
echo $dom->root->name;
echo "\n";
/* xmltree() creates a tree which is readonly. This means that a
   function like 
     $dom->root->new_child("TEST", "ljdf");
   won't work
*/

/* The following builds a xml document from scratch */
echo "Test 3: building a xml document from scratch\n";
$doc = new_xmldoc("1.0");
$root = $doc->add_root("HTML");
$head = $root->new_child("HEAD", "");
$head->new_child("TITLE", "Hier der Titel");
$body = $root->new_child("BODY", "");
$table = $body->new_child("TABLE", "");
$table->setattr("WIDTH", "100%");
$table->new_child("TR", " ");
echo $doc->dumpmem();

/* The following does some testing of the xpath support */
echo "Test 4: See if XPath works\n";
if(!$dom = xmldoc($xmlstr)) {
  echo "Error while parsing the document\n";
  exit;
}

if(false === ($xpathctx = xpath_new_context($dom))) {
	echo "Error in xpath_new_context()\n";
	exit;
}

/* What you get back is an object of type XPathObject.
   Depending on the sub type of XPathObject, the property
   'value' or 'nodeset' contains the result.
   The sub type is in property 'type'.
   See docs for libxml for valid types.
   1 means XPATH_NODESET which is in PHP an array of DomNodes.
*/
$xpathobj = xpath_eval($xpathctx, "/child::*");
echo $xpathobj->type."\n";
var_dump($xpathobj);
foreach($xpathobj->nodeset as $node)
	echo $node->name."\n";
?>