coldfusion + xml获取指定属性的节点

时间:2011-02-16 23:05:49

标签: xml coldfusion

给出以下XML:

<LiveScores>
    <Competition id="4915" name="Comp1" type="Union" group="">
        <Match id="1678" round="No Round" period="2" periodMinutes="40" matchTime="H2 40:06" previewArticleID="102948" matchReportArticleID="" status="Complete" alternateId="40790106">
            <MatchDate startDateLocal="2010-11-27 20:45:00" startDateUTC="2010-11-27 19:45:00" dayName="Sat" shortDate="27-Nov">2010-11-27 20:45:00</MatchDate>
        </Match>
        ...more matches
    </Competition>
    <Competition id="4916" name="Comp2" type="Union" group="">
        <Match id="1678" round="No Round" period="2" periodMinutes="40" matchTime="H2 40:06" previewArticleID="102948" matchReportArticleID="" status="Complete" alternateId="40790106">
            <MatchDate startDateLocal="2010-11-27 20:45:00" startDateUTC="2010-11-27 19:45:00" dayName="Sat" shortDate="27-Nov">2010-11-27 20:45:00</MatchDate>
        </Match>
        ...more matches
    </Competition>
    ...more competitions
</LiveScores>

给定指定的竞争ID(例如4915),如何仅返回具有此ID且没有其他竞争节点的竞赛节点?

1 个答案:

答案 0 :(得分:2)

解析xml,然后使用xpath函数运行XmlSearch查询。

<!--- parse the xml --->
<cfset variables.livescoresXML = xmlParse(xml) />
<!--- run the query for the competition node wit id of 4915 --->
<cfset variables.myElements = XmlSearch(
    variables.livescoresXML,
    "/LiveScores/Competition[@id=4915]"
 ) />