2011-02-17 148 views
3

XML技巧。我试图用目前正在进行的运动“比赛”或是即将到来的下一场比赛来分离节点。coldfusion + xml根据日期获取节点

thisScheduleXML:

<Data> 
     <Sport type="Union"> 
      <Schedules> 
       <Competition id="48" name="SR" type="Union" group=""> 
        <Match id="1684" round="Week 1" previewArticleID="" matchReportArticleID="" status="Upcoming" alternateId="1"> 
         <MatchDate startDateLocal="2011-02-18 19:35:00" dayName="Fri" shortDate="18-Feb">2011-02-18 19:35:00</MatchDate> 
         <Venue id="30" timeZoneID="NZDT" gmtMinutes="780" venue="Westpac Stadium" location="Wellington">Westpac Stadium, Wellington</Venue> 
         <HomeTeam id="8" alternateId="428">Hurricanes</HomeTeam> 
         <AwayTeam id="7" alternateId="427">Highlanders</AwayTeam> 
        </Match> 
        <Match id="1685" round="Week 1" previewArticleID="" matchReportArticleID="" status="Upcoming" alternateId="2"> 
         <MatchDate startDateLocal="2011-02-11 19:40:00" dayName="Fri" shortDate="18-Feb">2011-02-11 19:40:00</MatchDate> 
         <Venue id="160" timeZoneID="AEDT" gmtMinutes="660" venue="AAMI Park" location="Melbourne">AAMI Park, Melbourne</Venue> 
         <HomeTeam id="76" alternateId="0">Rebels</HomeTeam> 
         <AwayTeam id="12" alternateId="422">Waratahs</AwayTeam> 
        </Match> 
        .. more matches 
       </Competition> 
       ... more competitions 
      </Schedules> 
     </Sport> 
    </Data> 

在正确的方向任何帮助将不胜感激。 我还以为沿着这些线路的东西:

<cfset currentMatchNode = xmlSearch(thisScheduleXml,"/SportalData/Sport/Schedules/Match/MatchDate[@startLocalDate is current otherwise the next upcoming one]")> 
+0

由于XMLSearch()不支持xpath日期函数,因此不能单独使用xpath。 – orangepips 2011-02-18 16:52:09

回答

2

我无法找到一个方法来匹配日期的XPath。也许别人可以帮助。但是,在过去,我们已经使用循环在CF中处理了这个问题。例如:

<cfset nodes = xmlSearch(x, "/Data/Sport/Schedules/Competition/Match") /> 
<cfset found = false />  

<cfloop array="#nodes#" index="match"> 

    <!--- Check to see if the match is on now or in the future ---> 
    <cfif match["MatchDate"].xmlText gte now()> 
     <cfdump var="#match#" /> 
     <cfset found = true /> 
    </cfif> 

    <!--- Only output the first ---> 
    <cfif found> 
     <cfbreak /> 
    </cfif> 

</cfloop> 

这将打印第一场比赛即将到来的Match节点。希望它能帮助你朝正确的方向发展。

+0

有xpath日期功能。问题在于它们是XPath 2.0的一部分,看起来ColdFusion(至少8个)仅支持XPath 1.0。 这可以通过字符串解析在xpath中完成,但是问题会因时区而变得复杂。这里建议的cf代码可能是正确的方法,但需要修改以考虑时区。 http://www.w3schools.com/Xpath/xpath_functions.asp#datetime – Mark 2011-02-18 20:54:30