0

I am trying to retrieve course info to show Course title, Description and Location who have text that contains March and ignoring courses that are in April for instance.

Here is my XML:

 <div class="main"> 
    <div class="workshoplist-main">
        <div class="toggler pointer">
            <i class="icon-chevron-down"></i> 
            <span id="rpt_0_lblSectionHeader_0">Wednesday, March 18, 2015 (10:00 AM - 11:00 AM)</span> 
        </div>
        <div class="accordion-element">
            <div class="element">
                <p>Course title: Math 100</p>
                <p>Description: Math</p>
                <p>Location: SSD1</p>                   
            </div>
        </div>
        <div class="toggler pointer">
            <i class="icon-chevron-down"></i> 
            <span id="rpt_0_lblSectionHeader_0a">Wednesday, April 18, 2015 (10:00 AM - 11:00 AM)</span> 
        </div>
        <div class="accordion-element">
            <div class="element">
                <p>Course title: English 100</p>
                <p>Description: English</p>
                <p>Location: BBD1</p>                   
            </div>
        </div>
        <div class="toggler pointer">
            <i class="icon-chevron-down"></i> 
            <span id="rpt_0_lblSectionHeader_0b">Thursday, March 19, 2015 (10:00 AM - 11:00 AM)</span> 
        </div>
        <div class="accordion-element">
            <div class="element">
                <p>Course title: History 100</p>
                <p>Description: History</p>
                <p>Location: ADD</p>                    
            </div>
        </div>
    </div>
    <div class="workshoplist-main">
        <div class="toggler pointer">
            <i class="icon-chevron-down"></i> 
            <span id="rpt_0_lblSectionHeader_1">Thursday, April 16, 2015 (10:00 AM - 11:00 AM)</span> 
        </div>
        <div class="accordion-element">
            <div class="element">
                <p>Course title: Math 105</p>
                <p>Description: Math</p>
                <p>Location: SSD2</p>                   
            </div>
        </div>
    </div>
    <div class="workshoplist-main">
        <div class="toggler pointer">
            <i class="icon-chevron-down"></i> 
            <span id="rpt_0_lblSectionHeader_2">Friday, March 19, 2015 (10:00 AM - 11:00 AM)</span> 
        </div>
        <div class="accordion-element">
            <div class="element">
                <p>Course title: Chemistry 100</p>
                <p>Description: Chemistry</p>
                <p>Location: ADD</p>                    
            </div>
        </div>
    </div>  
</div>  

Here is my XPATH that I have written in PHP:

$details  = $node->xpath("//div[@class='toggler pointer']/span[contains(@id,'lblSectionHeader')][contains(text(), 'March')]/following-sibling::/*"); 

Result I would like to see is:

Wednesday, March 18, 2015 (10:00 AM - 11:00 AM)
   Course title: Math 100
   Description: Math
   Location: SSD1

Thursday, March 19, 2015 (10:00 AM - 11:00 AM)
   Course title: History 100
   Description: History
   Location: ADD

Do not show any April courses

The problem is I cannot get the

 <div class="element"> 

to retrieve the values for course, description, location

2
  • Since you're using XPath in PHP, I think we can assume that this is XPath 1.0 -- XPath 2.0 is not available. Correct me if that assumption is wrong. Commented Mar 19, 2015 at 18:39
  • following-sibling:: will select the next sibling of the context node As I see your context node is span at that point, so you gotta go back and put ../ before following sibling. Please try this and see if it works. Commented Mar 19, 2015 at 18:50

1 Answer 1

1

following-sibling:: cannot be immediately followed by /, so you should be getting an error. If you are seeing an error, you should tell us about it rather than omitting that clue. You could change the following-sibling part to ../following-sibling::*, but you would select much more than you want to.

I think what you need is to first select the spans with relevant dates, as you have done:

$details  = $node->xpath("//div[@class='toggler pointer']/
  span[contains(@id,'lblSectionHeader')][contains(text(), 'March')]");

Then iterate over them, and for each span, select

"(../following-sibling::div[@class='element'])[1]"

or

"(following::div[@class='element'])[1]"

Either one will give you the relevant div element, from which you can just select each <p> child.

If you're having trouble with this last step (extracting data from the <p> element children), show what code you tried, what the result was, and what result you wanted for that piece of code (you showed your desired overall result already).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.