How to fetch html string of XPath results?
NickName:oh_shi Ask DateTime:2011-08-26T04:10:47

How to fetch html string of XPath results?

Considering this code:

<div class="a">foo</div>
<div class="a"><div id="1">bar</div></div>

If I want to fetch all the values of divs with class a, I'll do the following query:

$q = $xpath->query('//div[@class="a"]');

However, I'll get this result:

foo
bar

But I want to get the actual value including the children tags. So it'll look like that:

foo
<div id="1">bar</div>

How can I accomplish that with XPath and DOMDocument only?


Solved by the function provided here.

Copyright Notice:Content Author:「oh_shi」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7196588/how-to-fetch-html-string-of-xpath-results

Answers
Marc B 2011-08-25T20:21:10

PHP DOM has an undocumented '.nodeValue' attribute which acts exactly like .innerHTML in a browser. Once you've used XPath to get the node you want, just do $node->nodeValue to get the innerhtml.",


Andrej 2011-08-25T20:20:10

You can try to use\n\n$xml = '<?xml version=\\'1.0\\' encoding=\\'UTF-8\\' ?>\n <root>\n <div class=\"a\">foo</div>\n <div class=\"a\"><div id=\"1\">bar</div></div>\n </root>';\n\n$xml = simplexml_load_string($xml); \nvar_dump($xml->xpath('//div[@class=\"a\"]'));\n\n\nBut in this case you will have to iterate objects.\n\nOutput:\n\narray(2) {\n [0]=>\n object(SimpleXMLElement)#2 (2) {\n [\"@attributes\"]=>\n array(1) {\n [\"class\"]=>\n string(1) \"a\"\n }\n [0]=>\n string(3) \"foo\"\n }\n [1]=>\n object(SimpleXMLElement)#3 (2) {\n [\"@attributes\"]=>\n array(1) {\n [\"class\"]=>\n string(1) \"a\"\n }\n [\"div\"]=>\n string(3) \"bar\"\n }\n }",


More about “How to fetch html string of XPath results?” related questions

How to fetch html string of XPath results?

Considering this code: &lt;div class="a"&gt;foo&lt;/div&gt; &lt;div class="a"&gt;&lt;div id="1"&gt;bar&lt;/div&gt;&lt;/div&gt; If I want to fetch all the v

Show Detail

Fetch partial string matched html tag using xpath

The html code is blind and It contains the string "PRICE" in html. That partial string has to be matched with html text.If the text matches(partial match) using xpath.It should return the particular

Show Detail

How to parse html string with xpath

How to select an image (or another HTML tag) with XPath in Go? resp, _ := http.Get(url) bytes, _ := ioutil.ReadAll(resp.Body) s := string(bytes)) how to parse s with XPath? like this code: lis...

Show Detail

Fetch a particular String in <div> using xpath

I have the following DOM structure / HTML: &lt;div class = "something" &gt; &lt;div&gt; &lt;span&gt;Some text&lt;/span&gt; &lt;/div&gt; "Automated Script"

Show Detail

How to Find an xpath with some string contained and some not contained in the div?(Xpath not contains)

**Problem :** How to Find an xpath with some string contained and some not contained in the div?(Xpath not contains)? **Below are three examples** 1)&lt;div&gt;You are now connected to Customer ...

Show Detail

How to pass an xpath to html_nodes()?

I want to use html_nodes to scrape organizations' names from the google search results (I need the first element only, assuming that that's gonna be the best guess). Right now, I am trying to targ...

Show Detail

How to fetch google search results links using selenium and python

I am trying to fetch all the search results link and print it. The problem I found is I am not able to iterate using the xpath of the individual search results because elems = driver.

Show Detail

html.xpath("//text()") in lxml

I am reading lxml tutorial with the following demonstration: #+BEGIN_SRC ipython :session lxml :results output print(html.xpath("string()")) # lxml.etree only! # TEXTTAIL print(html.xpath("//text(...

Show Detail

Yahoo pipe using XPath Fetch Page showing null

I'm building a Yahoo Pipe using the XPath Fetch Page module. You can see my pipe here. I can see the right information shown in the Debugger, showing 13 items, but when I run my pipe I'm getting the

Show Detail

Xpath php fetch links

I'm using this example to fetch links from a website : http://www.merchantos.com/makebeta/php/scraping-links-with-php/ $xpath = new DOMXPath($dom); $hrefs = $xpath-&gt;evaluate("/html/body//a"); ...

Show Detail