dom - PHP Xpath, help selecting a very specific node -


Similar question from last night, I do not have access to editing source HTML and I'm trying to parse a lot Compare to the price / comparison of the data product from the website. For the most part, this is working, but now I am trying to make it more efficient, faster and easier to read my spaghetti code.

I have the following test code; And whatever I want to do is return the attribute (for example, this whitewant) to the content if nodewell stock is done for the product type and nothing else.

  & lt; Div id = "productListing" & gt; & Lt; Div class = "productDetail" & gt; & Lt; Span class = "ProductType" & gt; Keeps & lt; / Span>: & lt; Span class = "productStock" & gt; & Lt; Span class = 'productContent' content = 'thisiswhatiwant' & gt; & Lt; / Span & gt; & Lt; / Span & gt; & Lt; / Div & gt; & Lt; Div class = "productDetail" & gt; & Lt; Span class = "ProductType" & gt; Non-stock & lt; / Span>: & lt; Span class = "productStock" & gt; & Lt; Span class = 'productContent' content = 'xyz' & gt; & Lt; / Span & gt; & Lt; / Span & gt; & Lt; / Div & gt; ... and lieutenant; Div class = "productDetail" & gt; & Lt; Span class = "ProductType" & gt; Non-stock & lt; / Span>: & lt; Span class = "productStock" & gt; & Lt; Span class = 'productContent' content = '123' & gt; & Lt; / Span & gt; & Lt; / Span & gt; & Lt; / Div & gt; & Lt; / Div & gt;  

It's missing the XPath query I have such far, but I did not click either something important or something in my mind just yet in the gear.

  // div [@ id = "productListing"] / div [@ class = "productDetail"] / span [@ class = "productStock"] / predecessor-brother :: period [text () = "Keeps"]  

Actually, what do I want to produce from the above test code:

   GetAttribute ('content'); ? & Gt; Output:?  

That is the domeedine object created with the given XML string:

  $ xpath = new domexpath ($ dome);  $ dom  $ Q = '// span [@ class = "productType" and text () = "statenced"] / ancestors :: * [@ class = "productDetail"] / span [@ class = "productstock"] / span [@ Class = "productContent"] '; $ Res = $ xpath- & gt; Query ($ q); Forex Currency ($ node as $ node) {echo $ node-> GetAttribute ('content') PHP_EOL; }  

Comments