2121use Cobweb \ExternalImport \Event \SubstructurePreprocessEvent ;
2222use Cobweb \ExternalImport \Exception \XpathSelectionFailedException ;
2323use Cobweb \ExternalImport \Importer ;
24+ use DOMNodeList ;
2425use Psr \EventDispatcher \EventDispatcherInterface ;
2526use TYPO3 \CMS \Core \Type \ContextualFeedbackSeverity ;
2627
@@ -69,10 +70,16 @@ public function handleData($rawData, Importer $importer): array
6970 $ records = [];
7071 if (array_key_exists ('nodepath ' , $ generalConfiguration )) {
7172 try {
72- $ records = $ this ->selectNodeWithXpath (
73+ $ records = $ this ->selectWithXpath (
7374 $ xPathObject ,
7475 $ generalConfiguration ['nodepath ' ]
7576 );
77+ // Only a node list makes sense in this context, log an error if anything else was returned
78+ if (!($ records instanceof \DomNodeList)) {
79+ $ importer ->addMessage (
80+ 'No nodes could be selected with existing configuration ' ,
81+ );
82+ }
7683 } catch (\Exception $ e ) {
7784 $ importer ->addMessage (
7885 $ e ->getMessage (),
@@ -201,11 +208,15 @@ public function getValue(\DOMNode $record, array $columnConfiguration, \DOMXPath
201208 $ selectedNode = $ nodeList ->item (0 );
202209 // If an XPath expression is defined, apply it (relative to currently selected node)
203210 if (!empty ($ columnConfiguration ['xpath ' ])) {
204- $ nodes = $ this ->selectNodeWithXpath (
211+ $ nodes = $ this ->selectWithXpath (
205212 $ xPathObject ,
206213 $ columnConfiguration ['xpath ' ],
207214 $ selectedNode
208215 );
216+ // If result is a string, return it as is
217+ if (is_string ($ nodes )) {
218+ return $ nodes ;
219+ }
209220 $ selectedNode = $ nodes ->item (0 );
210221 }
211222 $ value = $ this ->extractValueFromNode (
@@ -222,11 +233,15 @@ public function getValue(\DOMNode $record, array $columnConfiguration, \DOMXPath
222233 } else {
223234 // If an XPath expression is defined, apply it (relative to current node)
224235 if (!empty ($ columnConfiguration ['xpath ' ])) {
225- $ nodes = $ this ->selectNodeWithXpath (
236+ $ nodes = $ this ->selectWithXpath (
226237 $ xPathObject ,
227238 $ columnConfiguration ['xpath ' ],
228239 $ record
229240 );
241+ // If result is a string, return it as is
242+ if (is_string ($ nodes )) {
243+ return $ nodes ;
244+ }
230245 $ selectedNode = $ nodes ->item (0 );
231246 $ value = $ this ->extractValueFromNode (
232247 $ selectedNode ,
@@ -267,7 +282,7 @@ public function getNodeList(\DOMNode $record, array $columnConfiguration, \DOMXP
267282
268283 if ($ nodeList ->length > 0 && !empty ($ columnConfiguration ['xpath ' ])) {
269284 $ selectedNode = $ nodeList ->item (0 );
270- $ nodeList = $ this ->selectNodeWithXpath (
285+ $ nodeList = $ this ->selectWithXpath (
271286 $ xPathObject ,
272287 $ columnConfiguration ['xpath ' ],
273288 $ selectedNode
@@ -282,7 +297,7 @@ public function getNodeList(\DOMNode $record, array $columnConfiguration, \DOMXP
282297 } else {
283298 // If an XPath expression is defined, apply it (relative to current node)
284299 if (!empty ($ columnConfiguration ['xpath ' ])) {
285- $ nodeList = $ this ->selectNodeWithXpath (
300+ $ nodeList = $ this ->selectWithXpath (
286301 $ xPathObject ,
287302 $ columnConfiguration ['xpath ' ],
288303 $ record
@@ -291,13 +306,17 @@ public function getNodeList(\DOMNode $record, array $columnConfiguration, \DOMXP
291306 // Create a DOMNodeList by querying the current node (record) itself with XPath
292307 // (weird, but the alternative is to create a new DOMDocument and import the node into it,
293308 // which is not really any better)
294- $ nodeList = $ this ->selectNodeWithXpath (
309+ $ nodeList = $ this ->selectWithXpath (
295310 $ xPathObject ,
296311 '. ' ,
297312 $ record
298313 );
299314 }
300315 }
316+ // Only a node list makes sense in this context, create an empty list if anything else was returned
317+ if (!($ nodeList instanceof \DomNodeList)) {
318+ $ nodeList = new \DomNodeList ();
319+ }
301320 return $ nodeList ;
302321 }
303322
@@ -361,10 +380,10 @@ public function getXmlValue(\DOMNode $node): string
361380 * @param \DOMXPath $xPathObject Instantiated DOMXPath object
362381 * @param string $xPath XPath query to evaluate
363382 * @param \DOMNode|null $context Node giving the context of the XPath query (null for root node)
364- * @return \DOMNodeList List of found nodes
383+ * @return \DOMNodeList|string List of found nodes
365384 * @throws XpathSelectionFailedException
366385 */
367- public function selectNodeWithXpath (\DOMXPath $ xPathObject , string $ xPath , ?\DOMNode $ context = null ): \DOMNodeList
386+ public function selectWithXpath (\DOMXPath $ xPathObject , string $ xPath , ?\DOMNode $ context = null ): \DOMNodeList | string
368387 {
369388 $ resultNodes = $ xPathObject ->evaluate ($ xPath , $ context );
370389 if ($ resultNodes === false ) {
@@ -373,7 +392,7 @@ public function selectNodeWithXpath(\DOMXPath $xPathObject, string $xPath, ?\DOM
373392 1767541086
374393 );
375394 }
376- if ($ resultNodes ->length > 0 ) {
395+ if (( $ resultNodes instanceof \DOMNodeList && $ resultNodes ->length > 0 ) || is_string ( $ resultNodes ) ) {
377396 return $ resultNodes ;
378397 }
379398 throw new XpathSelectionFailedException (
0 commit comments