File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -763,6 +763,9 @@ use function VeeWee\Xml\Dom\Locator\document_element;
763
763
764
764
$doc = Document::fromXmlFile('some.xml');
765
765
$rootElement = $doc->locate(document_element());
766
+
767
+ // Since this is a common action, there is also a shortcut:
768
+ $doc->locateDocumentElement();
766
769
```
767
770
768
771
#### elements_with_namespaced_tagname
@@ -1116,7 +1119,12 @@ Converts the DOM document to something else.
1116
1119
use VeeWee\Xml\Dom\Document;
1117
1120
1118
1121
$doc = Document::fromXmlFile('some.xml');
1122
+
1123
+ // Get full XML including the XML declaration tag:
1119
1124
$xml = $doc->toXmlString();
1125
+
1126
+ // OR, get only the XML part without declaration:
1127
+ $xml = $doc->stringifyDocumentElement();
1120
1128
```
1121
1129
1122
1130
Instead of mapping a full document, you can also map a specific node only to XML.
@@ -1126,6 +1134,9 @@ use function VeeWee\Xml\Dom\Mapper\xml_string;
1126
1134
1127
1135
$mapper = xml_string();
1128
1136
$xml = $mapper($someNode);
1137
+
1138
+ // Or use the shortcut on Document level:
1139
+ $xml = $doc->stringifyNode($someNode);
1129
1140
```
1130
1141
1131
1142
#### xslt_template
Original file line number Diff line number Diff line change 6
6
7
7
use Closure ;
8
8
use DOMDocument ;
9
+ use DOMElement ;
9
10
use DOMNode ;
10
11
use DOMXPath ;
11
12
use VeeWee \Xml \Dom \Traverser \Traverser ;
@@ -113,6 +114,11 @@ public function locate(callable $locator)
113
114
return $ locator ($ this ->document );
114
115
}
115
116
117
+ public function locateDocumentElement (): DOMElement
118
+ {
119
+ return $ this ->locate (Locator \document_element ());
120
+ }
121
+
116
122
/**
117
123
* @param callable(DOMDocument): mixed $manipulator
118
124
*
@@ -191,4 +197,20 @@ public function toXmlString(): string
191
197
{
192
198
return $ this ->map (xml_string ());
193
199
}
200
+
201
+ /**
202
+ * @return non-empty-string
203
+ */
204
+ public function stringifyDocumentElement (): string
205
+ {
206
+ return xml_string ()($ this ->locateDocumentElement ());
207
+ }
208
+
209
+ /**
210
+ * @return non-empty-string
211
+ */
212
+ public function stringifyNode (DOMNode $ node ): string
213
+ {
214
+ return xml_string ()($ node );
215
+ }
194
216
}
Original file line number Diff line number Diff line change @@ -144,4 +144,34 @@ public function test_it_can_reconfigure_document(): void
144
144
static ::assertSame ($ doc1 ->toUnsafeDocument (), $ doc2 ->toUnsafeDocument ());
145
145
static ::assertXmlStringEqualsXmlString ($ xml1 , $ xml2 );
146
146
}
147
+
148
+ public function test_it_can_locate (): void
149
+ {
150
+ $ doc = Document::fromXmlString ('<hello /> ' );
151
+ $ actual1 = $ doc ->locate (document_element ());
152
+ $ actual2 = $ doc ->locateDocumentElement ();
153
+
154
+ static ::assertSame ($ doc ->toUnsafeDocument ()->documentElement , $ actual1 );
155
+ static ::assertSame ($ actual1 , $ actual2 );
156
+ }
157
+
158
+ public function test_it_can_stringify_parts (): void
159
+ {
160
+ $ doc = Document::fromXmlString ('<hello value="world"/> ' );
161
+ $ full = $ doc ->toXmlString ();
162
+ $ documentElement = $ doc ->stringifyDocumentElement ();
163
+ $ node = $ doc ->stringifyNode ($ doc ->locateDocumentElement ());
164
+ $ attr = $ doc ->stringifyNode ($ doc ->locateDocumentElement ()->getAttributeNode ('value ' ));
165
+
166
+
167
+ $ expected = '<hello value="world"/> ' ;
168
+
169
+ static ::assertSame (
170
+ '<?xml version="1.0"?> ' .PHP_EOL .$ expected .PHP_EOL ,
171
+ $ full
172
+ );
173
+ static ::assertSame ($ expected , $ documentElement );
174
+ static ::assertSame ($ expected , $ node );
175
+ static ::assertSame (' value="world" ' , $ attr );
176
+ }
147
177
}
You can’t perform that action at this time.
0 commit comments