N
N
Nikolai Konyukhov2015-07-16 10:40:04
PHP
Nikolai Konyukhov, 2015-07-16 10:40:04

php simple xml how to execute xpath per node?

When parsing unloading from 1s, I ran into a problem that xpath only works on the entire file, both when choosing a hard and relative path. However, the resulting element, which is also a simple_xml_element xpath object, refuses to work, even if I try to select all the nodes of the object through '//*'. What could be the problem, who knows.
As the respected krypt3r suggested : you must specify a relative path, do not use a root slash at the beginning.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Konyukhov, 2015-07-16
@heahoh

The necessary piece of the unloading is taken (products, categories, offers or something else)
$path = explode('/', $className->part);
$path = implode('/' . $this->ns, $path);
$this->xml->xpath('//' . $this->ns . $path);
For products
public $part = 'Catalog/Products';
Then
foreach ($this->xml[0] as $item) {
// Do something with the node.
// However, the xpath on the node no longer works
}
As it turned out, there were 2 problems:
1) for each node, you need to set the namespace
2) the xpath request to the node polls the entire parent element, that is, the xpath is executed to $xml instead of $item

K
krypt3r, 2015-07-16
@krypt3r

No test case, no code, nothing... What should we answer?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question