0

I need to extract every Sub Category url with its Name and Parent Category Name + its url from the data:

<ul class="root">
    <li class="category_container">
        <h1 class="category"><a href="/url/to/category1">Category 1</a></h1>
        <ul class="sub_category">
            <li><a class="sub_cat_link" href="/this/url/i/need/1a">Sub cat A</a></li>
            <li><a class="sub_cat_link" href="/this/url/i/need/1b">Sub cat B</a></li>
        </ul>
    </li>

    <li class="category_container">
        <h1 class="category"><a href="/url/to/category2">Category 2</a></h1>
        <ul class="sub_category">
            <li><a class="sub_cat_link" href="/this/url/i/need/2c">Sub cat C</a></li>
            <li><a class="sub_cat_link" href="/this/url/i/need/2d">Sub cat D</a></li>
        </ul>
    </li>
</ul>

and get that as array like this:

array (
    [0] => array ('cat' => 'Category 1', 'cat_url' => '/url/to/category1', 'sub_cat' => 'Sub cat A', 'subc_url' =>  '/this/url/i/need/1a')
    [1] => array ('cat' => 'Category 1', 'cat_url' => '/url/to/category2', 'sub_cat' => 'Sub cat B', 'subc_url' =>  '/this/url/i/need/1b')   
)


I can easily find node with "Sub Cat X" by using Document DOM and XPath but I don't know how to get the parent node name "Category 1" +link of a "Sub Cat X" that I found.
Maybe I should go with different approach and find "Category X" firstly and then dig in more for getting all its childs "Sub Cat X" nodes?
Please help with example commands that should be used.

Jimmix
  • 5,644
  • 6
  • 44
  • 71

1 Answers1

0

using context node as a second parameter of DOMXPath::query is a solution, simply find firstly

<li class="category_container">

and iterate through returned object by foreach with searching again by using XPath that describes inner nodes and context node object (li class="category_container" in this case) as second parameter
Some examples here:
How do I do an XPath query on a DOMNode?

Community
  • 1
  • 1
Jimmix
  • 5,644
  • 6
  • 44
  • 71