@@ -27,6 +27,11 @@ class Crawler extends \SplObjectStorage
27
27
*/
28
28
protected $ uri ;
29
29
30
+ /**
31
+ * @var string The default namespace prefix to be used with XPath and CSS expressions
32
+ */
33
+ private $ defaultNamespacePrefix = 'default ' ;
34
+
30
35
/**
31
36
* Constructor.
32
37
*
@@ -708,6 +713,16 @@ public function form(array $values = null, $method = null)
708
713
return $ form ;
709
714
}
710
715
716
+ /**
717
+ * Overloads a default namespace prefix to be used with XPath and CSS expressions.
718
+ *
719
+ * @param string $prefix
720
+ */
721
+ public function setDefaultNamespacePrefix ($ prefix )
722
+ {
723
+ $ this ->defaultNamespacePrefix = $ prefix ;
724
+ }
725
+
711
726
/**
712
727
* Converts string for XPath expressions.
713
728
*
@@ -806,7 +821,7 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = array(
806
821
807
822
foreach ($ prefixes as $ prefix ) {
808
823
// ask for one namespace, otherwise we'd get a collection with an item for each node
809
- $ namespaces = $ domxpath ->query (sprintf ('(//namespace::*[name()="%s"])[last()] ' , ' default ' === $ prefix ? '' : $ prefix ));
824
+ $ namespaces = $ domxpath ->query (sprintf ('(//namespace::*[name()="%s"])[last()] ' , $ this -> defaultNamespacePrefix === $ prefix ? '' : $ prefix ));
810
825
if ($ node = $ namespaces ->item (0 )) {
811
826
$ domxpath ->registerNamespace ($ prefix , $ node ->nodeValue );
812
827
} else {
@@ -824,7 +839,7 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = array(
824
839
*/
825
840
private function findNamespacePrefixes ($ xpath )
826
841
{
827
- if (preg_match_all ('/(?P<prefix>[a-zA-Z_][a-zA-Z_0-9\-\.]+ ):[^:]/ ' , $ xpath , $ matches )) {
842
+ if (preg_match_all ('/(?P<prefix>[a-zA-Z_][a-zA-Z_0-9\-\.]* ):[^:]/ ' , $ xpath , $ matches )) {
828
843
return array_unique ($ matches ['prefix ' ]);
829
844
}
830
845
0 commit comments