PHP Xpath表达式无效

时间:2015-03-19 17:53:33

标签: php xml xpath

我正在尝试检索课程信息,以显示课程标题,说明和位置,其中包含包含March的文本,并忽略4月份的课程。

这是我的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>  

这是我用PHP编写的XPATH:

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

我希望看到的结果是:

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

不要显示任何四月课程

问题是我无法得到

 <div class="element"> 

检索课程,描述,位置的值

1 个答案:

答案 0 :(得分:1)

following-sibling::无法立即跟随/,因此您应该收到错误消息。如果您看到错误,您应该告诉我们,而不是省略该线索。您可以将以下兄弟部分更改为../following-sibling::*,但您可以选择比您想要的更多。

我认为您需要的是首先选择具有相关日期的跨度,如您所做:

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

然后迭代它们,对于每个跨度,选择

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

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

任何一个都会给你相关的div元素,你可以从中选择每个<p>孩子。

如果您在最后一步(从<p>元素子元素中提取数据)遇到问题,请显示您尝试的代码,结果是什么,以及您希望为该代码提供什么结果(您已经显示了所需的整体结果)。