[ create a new paste ] login | about

Link: http://codepad.org/EuFAX3cw    [ raw code | output | fork ]

PHP, pasted on Jan 4:
<?php error_reporting(-1);


// http://stackoverflow.com/questions/4592148/parsing-html-with-xpath-and-php

$xml = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<body>
	<p>Not in a table and before first h1</p>
	<table>
		<p>In a table</p>
	</table>
	<p>This one is good too</p>
	<h1>First Heading</h1>
	<p>Not in a table but to late in document</p>
	<table>
		<p>In a table</p>
	</table>
	<h1>Second Heading</h1>
	<p>Not in a table but to late in document</p>
	<table>
		<p>In a table</p>
	</table>
</body>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
$xp = new DOMXPath($dom);
$expr = '//p[not(preceding::h1[1]) and not(ancestor::table)]';
foreach ($xp->query($expr) as $node) {
    echo $dom->saveXml($node);
}


Output:
1
<p>Not in a table and before first h1</p><p>This one is good too</p>


Create a new paste based on this one


Comments: