1
|
|
|
<?php |
2
|
|
|
/** @file |
3
|
|
|
* XML extensions. See QPXML. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace QueryPath\Extension; |
7
|
|
|
|
8
|
|
|
use QueryPath\DOMQuery; |
9
|
|
|
use QueryPath\QueryPath; |
10
|
|
|
use QueryPath\Extension; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Provide QueryPath with additional XML tools. |
14
|
|
|
* |
15
|
|
|
* @author M Butcher <[email protected]> |
16
|
|
|
* @author Xander Guzman <[email protected]> |
17
|
|
|
* @license MIT |
18
|
|
|
* @see QueryPath::Extension |
19
|
|
|
* @see QueryPath::ExtensionRegistry::extend() |
20
|
|
|
* @see QPXML |
21
|
|
|
* @ingroup querypath_extensions |
22
|
|
|
*/ |
23
|
|
|
class QPXML implements Extension |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
protected $qp; |
27
|
|
|
|
28
|
|
|
public function __construct(\QueryPath\Query $qp) |
29
|
|
|
{ |
30
|
|
|
$this->qp = $qp; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function schema($file) |
34
|
|
|
{ |
35
|
|
|
$doc = $this->qp->branch()->top()->get(0)->ownerDocument; |
|
|
|
|
36
|
|
|
|
37
|
|
|
if (!$doc->schemaValidate($file)) { |
38
|
|
|
throw new \QueryPath\Exception('Document did not validate against the schema.'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get or set a CDATA section. |
44
|
|
|
* |
45
|
|
|
* If this is given text, it will create a CDATA section in each matched element, |
46
|
|
|
* setting that item's value to $text. |
47
|
|
|
* |
48
|
|
|
* If no parameter is passed in, this will return the first CDATA section that it |
49
|
|
|
* finds in the matched elements. |
50
|
|
|
* |
51
|
|
|
* @param string $text |
52
|
|
|
* The text data to insert into the current matches. If this is NULL, then the first |
53
|
|
|
* CDATA will be returned. |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
* If $text is not NULL, this will return a {@link QueryPath}. Otherwise, it will |
57
|
|
|
* return a string. If no CDATA is found, this will return NULL. |
58
|
|
|
* @see comment() |
59
|
|
|
* @see QueryPath::text() |
60
|
|
|
* @see QueryPath::html() |
61
|
|
|
*/ |
62
|
|
|
public function cdata($text = NULL) |
63
|
|
|
{ |
64
|
|
|
if (isset($text)) { |
65
|
|
|
// Add this text as CDATA in the current elements. |
66
|
|
|
foreach ($this->qp->get() as $element) { |
|
|
|
|
67
|
|
|
$cdata = $element->ownerDocument->createCDATASection($text); |
68
|
|
|
$element->appendChild($cdata); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->qp;; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Look for CDATA sections. |
75
|
|
|
foreach ($this->qp->get() as $ele) { |
76
|
|
|
foreach ($ele->childNodes as $node) { |
77
|
|
|
if ($node->nodeType === XML_CDATA_SECTION_NODE) { |
78
|
|
|
// Return first match. |
79
|
|
|
return $node->textContent; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return NULL; |
85
|
|
|
// Nothing found |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get or set a comment. |
90
|
|
|
* |
91
|
|
|
* This function is used to get or set comments in an XML or HTML document. |
92
|
|
|
* If a $text value is passed in (and is not NULL), then this will add a comment |
93
|
|
|
* (with the value $text) to every match in the set. |
94
|
|
|
* |
95
|
|
|
* If no text is passed in, this will return the first comment in the set of matches. |
96
|
|
|
* If no comments are found, NULL will be returned. |
97
|
|
|
* |
98
|
|
|
* @param string $text |
99
|
|
|
* The text of the comment. If set, a new comment will be created in every item |
100
|
|
|
* wrapped by the current {@link QueryPath}. |
101
|
|
|
* @return mixed |
102
|
|
|
* If $text is set, this will return a {@link QueryPath}. If no text is set, this |
103
|
|
|
* will search for a comment and attempt to return the string value of the first |
104
|
|
|
* comment it finds. If no comment is found, NULL will be returned. |
105
|
|
|
* @see cdata() |
106
|
|
|
*/ |
107
|
|
|
public function comment($text = NULL) |
108
|
|
|
{ |
109
|
|
|
if (isset($text)) { |
110
|
|
|
foreach ($this->qp->get() as $element) { |
111
|
|
|
$comment = $element->ownerDocument->createComment($text); |
112
|
|
|
$element->appendChild($comment); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->qp; |
116
|
|
|
} |
117
|
|
|
foreach ($this->qp->get() as $ele) { |
118
|
|
|
foreach ($ele->childNodes as $node) { |
119
|
|
|
if ($node->nodeType == XML_COMMENT_NODE) { |
120
|
|
|
// Return first match. |
121
|
|
|
return $node->textContent; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get or set a processor instruction. |
129
|
|
|
*/ |
130
|
|
|
public function pi($prefix = NULL, $text = NULL) |
131
|
|
|
{ |
132
|
|
|
if (isset($text)) { |
133
|
|
|
foreach ($this->qp->get() as $element) { |
134
|
|
|
$comment = $element->ownerDocument->createProcessingInstruction($prefix, $text); |
135
|
|
|
$element->appendChild($comment); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->qp; |
139
|
|
|
} |
140
|
|
|
foreach ($this->qp->get() as $ele) { |
141
|
|
|
foreach ($ele->childNodes as $node) { |
142
|
|
|
if ($node->nodeType == XML_PI_NODE) { |
143
|
|
|
|
144
|
|
|
if (isset($prefix)) { |
145
|
|
|
if ($node->tagName == $prefix) { |
146
|
|
|
return $node->textContent; |
147
|
|
|
} |
148
|
|
|
} else { |
149
|
|
|
// Return first match. |
150
|
|
|
return $node->textContent; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} // foreach |
154
|
|
|
} // foreach |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function toXml() |
158
|
|
|
{ |
159
|
|
|
return $this->qp->document()->saveXml(); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Create a NIL element. |
164
|
|
|
* |
165
|
|
|
* @param string $text |
166
|
|
|
* @param string $value |
167
|
|
|
* @reval object $element |
168
|
|
|
*/ |
169
|
|
|
public function createNilElement($text, $value) |
170
|
|
|
{ |
171
|
|
|
$value = ($value) ? 'true' : 'false'; |
172
|
|
|
$element = $this->qp->createElement($text); |
|
|
|
|
173
|
|
|
$element->attr('xsi:nil', $value); |
174
|
|
|
|
175
|
|
|
return $element; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Create an element with the given namespace. |
180
|
|
|
* |
181
|
|
|
* @param string $text |
182
|
|
|
* @param string $nsUri |
183
|
|
|
* The namespace URI for the given element. |
184
|
|
|
* @return \QueryPath\DOMQuery |
185
|
|
|
*/ |
186
|
|
|
public function createElement($text, $nsUri = NULL) |
187
|
|
|
{ |
188
|
|
|
if (isset ($text)) { |
189
|
|
|
foreach ($this->qp->get() as $element) { |
190
|
|
|
if ($nsUri === NULL && strpos($text, ':') !== false) { |
191
|
|
|
$ns = array_shift(explode(':', $text)); |
|
|
|
|
192
|
|
|
$nsUri = $element->ownerDocument->lookupNamespaceURI($ns); |
193
|
|
|
|
194
|
|
|
if ($nsUri === NULL) { |
195
|
|
|
throw new \QueryPath\Exception("Undefined namespace for: " . $text); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$node = NULL; |
200
|
|
|
if ($nsUri !== NULL) { |
201
|
|
|
$node = $element->ownerDocument->createElementNS( |
202
|
|
|
$nsUri, |
203
|
|
|
$text |
204
|
|
|
); |
205
|
|
|
} else { |
206
|
|
|
$node = $element->ownerDocument->createElement($text); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return QueryPath::with($node); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return new DOMQuery(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Append an element. |
218
|
|
|
* |
219
|
|
|
* @param string $text |
220
|
|
|
* @return \QueryPath\DOMQuery |
221
|
|
|
*/ |
222
|
|
|
public function appendElement($text) |
223
|
|
|
{ |
224
|
|
|
if (isset ($text)) { |
225
|
|
|
foreach ($this->qp->get() as $element) { |
226
|
|
|
$node = $this->qp->createElement($text); |
227
|
|
|
QueryPath::with($element)->append($node); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this->qp; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|