Answer the question
In order to leave comments, you need to log in
How to get to the middle of the text using xpath in php?
Friends, tell me how to get to the middle of the text, decorated with paragraphs, using xpath? The implode-explode method is not suitable, since some paragraphs are in the blockquote tag, they do not need to be considered. It turns out that as an argument in //p[i] xpath, instead of i, substitute the sum of all elements p (paragraph), excluding those that are in the blockquote tag and divide it in half, but there can still be a non-integer argument, it also needs to be rounded .. .
Answer the question
In order to leave comments, you need to log in
So you did almost everything yourself. In order not to take those paragraphs that are inside the blockquote, you need to transform your xpath a little:
//p[position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5)][not(parent::blockquote)]
XPath is a way to get to an element, but not to edit the DOM.
Ivan Koryukov , well, in php, you can add an element to the DOM with it, here's a small example:
$xml = new DomDocument();
$xml->preserveWhitespace = false;
$xml->load('contacts.xml');
$xpath = new DOMXPath($xml);
$eva = $xpath->query('/contacts/person[.="Eva"]')->item(0);
$john = $xml->createElement('person', 'John');
$eva->parentNode->insertBefore($john, $eva->nextSibling);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question