Skip to content

Commit da136df

Browse files
author
Harrison Ifeanyichukwu
committed
feat: add ability to select nodes using alternate node models to simplify feed parsing and fallbacks
1 parent e31afbd commit da136df

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/XPath.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@ public function selectNodes(string $expression, DOMNode $context_node = null)
6464
return $result;
6565
}
6666

67+
/**
68+
* queries the document and returns a single result item or null
69+
*
70+
*@return DOMNode|null
71+
*/
72+
public function selectAltNode(string $expressions, DOMNode $context_node = null)
73+
{
74+
foreach(preg_split('/\s*\|\|\s*/', $expressions) as $expression) {
75+
$result = $this->selectNode($expression, $context_node);
76+
if ($result)
77+
return $result;
78+
}
79+
80+
return null;
81+
}
82+
83+
/**
84+
* queries the document and returns a DOMNodeList or null
85+
*
86+
*@return DOMNodeList|null
87+
*/
88+
public function selectAltNodes(string $expressions, DOMNode $context_node = null)
89+
{
90+
foreach(preg_split('/\s*\|\|\s*/', $expressions) as $expression) {
91+
$result = $this->selectNodes($expression, $context_node);
92+
if ($result && $result->length > 0)
93+
return $result;
94+
}
95+
96+
return null;
97+
}
98+
6799
/**
68100
* returns the dom xpath
69101
*

0 commit comments

Comments
 (0)