2015-09-10 117 views
0

我在這個XML的文件格式:選擇基於屬性的特定屬性值

<league country="argentina" name="Argentina: Torneo Federal A - Winners Stage" cup="False" id="2909" sub_id="29094"> 
    <match alternate_id="4187269" alternate_id_2="4394549" date="14.08.2015" id="4304775" static_id="14081523515562337775" status="FT" time="18:00"> 
    <home goals="2" id="2351556" name="Club Atletico Mitre"></home> 
    <away goals="1" id="2337775" name="Chaco For Ever"></away> 
    <events> 
    <event assist="" assistid="" id="22958994" minute="32" player="Juan Pablo Villafane" playerid="" result="[1 - 0]" team="home" type="goal"></event> 
    <event assist="" assistid="" id="22958995" minute="48" player="Aldo Visconti" playerid="" result="[1 - 1]" team="away" type="goal"></event> 
    <event assist="" assistid="" id="22958996" minute="90" player="Emanuel Lazzarini" playerid="" result="[2 - 1]" team="home" type="goal"></event> 
    </events> 
    <ht score="[1-0]"></ht> 
    <ft score="[2-1]"></ft> 
    </match></league><league country="argentina" name="Argentina: Torneo Federal A - Losers Stage" cup="False" id="2909" sub_id="29095"><match alternate_id="4178253" alternate_id_2="4410516" date="14.08.2015" id="4295774" static_id="14081523557572337894" status="FT" time="18:00"> 
    <home goals="2" id="2355757" name="Velez de San Ramon"></home> 
    <away goals="1" id="2337894" name="Sportivo Patria"></away> 
    <events> 
    <event assist="" assistid="" id="23118664" minute="11" player="Diego Suarez" playerid="" result="[1 - 0]" team="home" type="goal"></event> 
    <event assist="" assistid="" id="23118665" minute="64" player="Luis Leguizamon" playerid="" result="[2 - 0]" team="home" type="goal"></event> 
    <event assist="" assistid="" id="23118666" minute="67" player="Rodrigo Ramirez (o.g.)" playerid="" result="[2 - 1]" team="away" type="goal"></event> 
    </events> 
    <ht score="[1-0]"></ht> 
    <ft score="[2-1]"></ft> 

這是我的控制器:

xml = Nokogiri::XML(f) 

    path = "//match[@static_id='user.matchid']" 
    @country = xml.xpath(path) 

我怎樣才能得到團隊的名稱,依據條件是屬性值"result" = [1-0][0-1]

+0

歡迎堆棧溢出。當您提供數據或代碼時,將其降至必要的最低水平,以顯示您遇到的問題。換句話說,你的XML非常冗長,需要減少。這也是無效的。當Nokogiri _can_修復它時,這樣做會導致XML通常無法準確反映原始內容。未能提供準確的反映真實信息的輸入數據會減慢或停止我們幫助您的能力,因此請幫助我們爲您提供幫助並提供準確,最簡單的數據。 –

回答

0

我會做這樣的事情:

require 'nokogiri' 

doc = Nokogiri::XML(<<EOT) 
    <xml> 
    <league> 
     <match> 
     <events> 
      <event player="Juan Pablo Villafane" result="[1 - 0]" team="home"></event> 
      <event player="Aldo Visconti" result="[1 - 1]" team="away"></event> 
     </events> 
     <events> 
      <event player="Diego Suarez" result="[1 - 0]" team="home"></event> 
      <event player="Luis Leguizamon" result="[2 - 0]" team="home"></event> 
     </events> 
     </match> 
    </league> 
    </xml> 
EOT 

doc.search('//event[contains(@result, "[1 - 0]") or contains(@result, "[0 - 1]")]/@team').map(&:text) 
# => ["home", "home"] 
0

沒有明確說明什麼是檢查究竟是。所以,這裏是上下文的示例情況,假設你想獲得主隊比賽的名稱,其中最後一次得分是"[2-1]"

//match[ft/@score = '[2-1]']/home/@name