class DOMXPath (View source)

The DOMXPath class (supports XPath 1.0)

Properties

DOMDocument $document
$registerNodeNamespaces

Methods

__construct(DOMDocument $document, bool $registerNodeNS = true)

Creates a new DOMXPath object

bool
registerNamespace(string $prefix, string $namespace)

Registers the namespace with the DOMXPath object

mixed
query(string $expression, DOMNode $contextNode = null, bool $registerNodeNS = true)

Evaluates the given XPath expression

mixed
evaluate(string $expression, DOMNode $contextNode = null, bool $registerNodeNS = true)

Evaluates the given XPath expression and returns a typed result if possible.

void
registerPhpFunctions(string|string[] $restrict = null)

Register PHP functions as XPath functions

void
registerPhpFunctionNS(string $namespaceURI, string $name, callable $callable)

No description

static string
quote(string $str)

No description

Details

__construct(DOMDocument $document, bool $registerNodeNS = true)

Creates a new DOMXPath object

Parameters

DOMDocument $document

The DOMDocument associated with the DOMXPath.

bool $registerNodeNS

[optional] allow global flag to configure query() or evaluate() calls. Since 8.0.

bool registerNamespace(string $prefix, string $namespace)

Registers the namespace with the DOMXPath object

Parameters

string $prefix

The prefix.

string $namespace

The URI of the namespace.

Return Value

bool

true on success or false on failure.

mixed query(string $expression, DOMNode $contextNode = null, bool $registerNodeNS = true)

Evaluates the given XPath expression

Parameters

string $expression

The XPath expression to execute.

DOMNode $contextNode

[optional]

The optional contextnode can be specified for doing relative XPath queries. By default, the queries are relative to the root element.

bool $registerNodeNS

[optional]

The optional registerNodeNS can be specified to disable automatic registration of the context node.

Return Value

mixed

a DOMNodeList containing all nodes matching the given XPath expression. Any expression which does not return nodes will return an empty DOMNodeList. The return is false if the expression is malformed or the contextnode is invalid.

mixed evaluate(string $expression, DOMNode $contextNode = null, bool $registerNodeNS = true)

Evaluates the given XPath expression and returns a typed result if possible.

Parameters

string $expression

The XPath expression to execute.

DOMNode $contextNode

[optional]

The optional contextnode can be specified for doing relative XPath queries. By default, the queries are relative to the root element.

bool $registerNodeNS

[optional]

The optional registerNodeNS can be specified to disable automatic registration of the context node.

Return Value

mixed

a typed result if possible or a DOMNodeList<DOMNode|DOMNameSpaceNode>|false containing all nodes matching the given XPath expression.

void registerPhpFunctions(string|string[] $restrict = null)

Register PHP functions as XPath functions

Parameters

string|string[] $restrict

[optional]

Use this parameter to only allow certain functions to be called from XPath.

This parameter can be either a string (a function name) or an array of function names.

Return Value

void

void registerPhpFunctionNS(string $namespaceURI, string $name, callable $callable)

Since: 8.4

No description

Parameters

string $namespaceURI
string $name
callable $callable

Return Value

void

static string quote(string $str)

Since: 8.4

No description

Parameters

string $str

Return Value

string