xref: /PHP-8.1/ext/dom/tests/canonicalization.phpt (revision bd9f4fa6)
1--TEST--
2Test: Canonicalization - C14N()
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7$xml = <<<EOXML
8<?xml version="1.0" encoding="ISO-8859-1" ?>
9<foo xmlns="http://www.example.com/ns/foo"
10     xmlns:fubar="http://www.example.com/ns/fubar" xmlns:test="urn::test"><contain>
11  <bar><test1 /></bar>
12  <bar><test2 /></bar>
13  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test3 /></fubar:bar>
14  <fubar:bar><test4 /></fubar:bar>
15<!-- this is a comment -->
16</contain>
17</foo>
18EOXML;
19
20$dom = new DOMDocument();
21$dom->loadXML($xml);
22$doc = $dom->documentElement->firstChild;
23
24/* inclusive/without comments first child element of doc element is context. */
25echo $doc->C14N()."\n\n";
26
27/* exclusive/without comments first child element of doc element is context. */
28echo $doc->c14N(TRUE)."\n\n";
29
30/* inclusive/with comments first child element of doc element is context. */
31echo $doc->C14N(FALSE, TRUE)."\n\n";
32
33/* exclusive/with comments first child element of doc element is context. */
34echo $doc->C14N(TRUE, TRUE)."\n\n";
35
36/* exclusive/without comments using xpath query. */
37echo $doc->c14N(TRUE, FALSE, array('query'=>'(//. | //@* | //namespace::*)'))."\n\n";
38
39/* exclusive/without comments first child element of doc element is context.
40   using xpath query with registered namespace.
41   test namespace prefix is also included. */
42echo $doc->c14N(TRUE, FALSE,
43                array('query'=>'(//a:contain | //a:bar | .//namespace::*)',
44                      'namespaces'=>array('a'=>'http://www.example.com/ns/foo')),
45                array('test'))."\n\n";
46
47/* exclusive/without comments first child element of doc element is context.
48   test namespace prefix is also included */
49echo $doc->C14N(TRUE, FALSE, NULL, array('test'));
50?>
51--EXPECT--
52<contain xmlns="http://www.example.com/ns/foo" xmlns:fubar="http://www.example.com/ns/fubar" xmlns:test="urn::test">
53  <bar><test1></test1></bar>
54  <bar><test2></test2></bar>
55  <fubar:bar><test3></test3></fubar:bar>
56  <fubar:bar><test4></test4></fubar:bar>
57
58</contain>
59
60<contain xmlns="http://www.example.com/ns/foo">
61  <bar><test1></test1></bar>
62  <bar><test2></test2></bar>
63  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test3></test3></fubar:bar>
64  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test4></test4></fubar:bar>
65
66</contain>
67
68<contain xmlns="http://www.example.com/ns/foo" xmlns:fubar="http://www.example.com/ns/fubar" xmlns:test="urn::test">
69  <bar><test1></test1></bar>
70  <bar><test2></test2></bar>
71  <fubar:bar><test3></test3></fubar:bar>
72  <fubar:bar><test4></test4></fubar:bar>
73<!-- this is a comment -->
74</contain>
75
76<contain xmlns="http://www.example.com/ns/foo">
77  <bar><test1></test1></bar>
78  <bar><test2></test2></bar>
79  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test3></test3></fubar:bar>
80  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test4></test4></fubar:bar>
81<!-- this is a comment -->
82</contain>
83
84<foo xmlns="http://www.example.com/ns/foo"><contain>
85  <bar><test1></test1></bar>
86  <bar><test2></test2></bar>
87  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test3></test3></fubar:bar>
88  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test4></test4></fubar:bar>
89
90</contain>
91</foo>
92
93<contain xmlns="http://www.example.com/ns/foo" xmlns:test="urn::test"><bar></bar><bar></bar></contain>
94
95<contain xmlns="http://www.example.com/ns/foo" xmlns:test="urn::test">
96  <bar><test1></test1></bar>
97  <bar><test2></test2></bar>
98  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test3></test3></fubar:bar>
99  <fubar:bar xmlns:fubar="http://www.example.com/ns/fubar"><test4></test4></fubar:bar>
100
101</contain>
102