Php 使用xpath解析xml

Php 使用xpath解析xml,php,simplexml,Php,Simplexml,我有以下xml文档: <DynaCal> <Events> <Event> <ShortDesc>7th Grade Volleyball BBC Tournament: Pettisville vs. Hilltop/Edon winner in Match 4 of the day. Match 1 begins at 9:00 AM</ShortDesc> <Date>10/15/

我有以下xml文档:

<DynaCal>
  <Events>
    <Event>
      <ShortDesc>7th Grade Volleyball BBC Tournament: Pettisville vs. Hilltop/Edon winner in Match 4 of the day.  Match 1 begins at 9:00 AM</ShortDesc>
      <Date>10/15/2011</Date>
      <LocationDesc>Fayette High School</LocationDesc>
      <Time>9:00 AM</Time>
    </Event>

    <Event>
      <ShortDesc>8th Grade Volleyball BBC Tournament: Pettisville vs. Stryker/North Central winner in Match 3 of the day.  Match 1 begins at 9:00 AM</ShortDesc>
      <Date>10/15/2011</Date>
      <LocationDesc>Fayette High School</LocationDesc>
      <Time>9:00 AM</Time>
    </Event>

    <Event>
      <ShortDesc>Varsity Cross Country Practice</ShortDesc>
      <Date>10/15/2011</Date>
      <LocationDesc>Oak Openings</LocationDesc>
      <Time>9:00 AM - 11:30 AM</Time>
    </Event>

    <Event>
      <ShortDesc>Varsity Cross Country Practice</ShortDesc>
      <Date>10/17/2011</Date>
      <LocationDesc>Pettisville School</LocationDesc>
      <Time>3:15 PM - 5:30 PM</Time>
    </Event>

    <Event>
      <ShortDesc>Varsity Volleyball Practice</ShortDesc>
      <Date>10/17/2011</Date>
      <LocationDesc>Pettisville Varsity Gym</LocationDesc>
      <Time>3:30 PM - 5:30 PM</Time>
    </Event>

    <Event>
      <ShortDesc>Varsity Cross Country Practice</ShortDesc>
      <Date>10/18/2011</Date>
      <LocationDesc>Oak Openings</LocationDesc>
      <Time>3:15 PM - 5:30 PM</Time>
    <Event>
  </Events>
</DynaCal>

英国广播公司7年级排球锦标赛:佩蒂斯维尔vs.希尔顿/埃顿在当天第4场比赛中获胜。第一场比赛上午9点开始
10/15/2011
费耶特高中
上午9点
英国广播公司8年级排球锦标赛:佩蒂斯维尔vs史崔克/中北部在当天的第3场比赛中获胜。第一场比赛上午9点开始
10/15/2011
费耶特高中
上午9点
大学越野训练
10/15/2011
橡木开口
上午9:00-11:30
大学越野训练
10/17/2011
佩蒂斯维尔学校
下午三时十五分至五时三十分
大学排球训练
10/17/2011
佩蒂斯维尔大学体育馆
下午三时三十分至五时三十分
大学越野训练
10/18/2011
橡木开口
下午三时十五分至五时三十分
文档,并希望显示它,以便显示具有相同日期的所有事件

<h4>Date 1</h4>
<p>ShortDesc</p>
<p>Time</p>
<p>ShortDesc</p>
<p>Time</p>

<h4>Date 2</h4>
<p>ShortDesc</p>
<p>Time</p>
<p>ShortDesc</p>
<p>Time</p>
日期1
ShortDesc

时间

ShortDesc

时间

日期2 ShortDesc

时间

ShortDesc

时间


我可以很容易地解析xml并提取这些项目,我对php非常陌生,不知道如何设置数组来查找子元素(日期)并显示该日期内的所有事件,然后转到下一个日期

分部分执行此操作。首先,获取每个事件,然后在PHP中消除重复。大概是这样的:

$values = array();
$xml = simplexml_load_file($file);
foreach ($xml->xpath('/DynaCal/Events/Event') as $event)
{
    // Add your values to an array here
    $values[]['shortDesc'] = $event->ShortDesc;

    // Then dedup here (add items conditionally depending on date)
}

// Or dedup here after the loop is done... :)

嗨,如果我的答案对你有帮助,你会选择它作为正确答案吗?这将为回答问题的用户分配一些信誉点-谢谢。