2010-11-30 179 views
0

我嘗試下面解析結構成套旅程選項,這樣我可以找出所有可能的方式獲得距離Pontypridd到蘭戈倫和背部。的XPath:選擇以下所有節點,直到某個節點

使用XPath,我可以做//div[@class='JourneyOptions']來選擇實際包含旅程信息的所有行。在XPath之外,我可以遍歷每一行來決定是否應將其添加到一組旅程中,或者它是否是一組新旅程中的第一個。

在下面的示例中,所有的旅程集將包含兩個行程,而是一組可以包含僅一個旅程(「直接」旅程),或兩個以上的(一個以上的「連接」)。

是否有XPath表達式來選擇第一個出站集的所有行程,第二個出站集的所有行程等等?

每個集合中的第一個旅程都有一個帶有整數值的無線電輸入。我可以動態生成這些來標記每個集合,但是需要知道何時停止生成(或者只是等待XPath失敗)。

<div class='TableHolder'> 

    <p>...</p> 
    <h2 id='DirectionHeader'>Outbound Options</h2> 
    <p>Pontypridd to Llangollen, 30/11/1910</p> 

    <!-- first part of the first journey from Pontypridd to Llangollen --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ColumnOne'> 
      <input type='radio' checked='checked' name='out' value='1'> 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    <div> 

    <!-- second part of the first journey from Pontypridd to Llangollen --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ConnectingJournies'> 
      <p>...</p> 
     </div> 
     <div class='ColumnOne'> 
      ... doesn't contain a radio input ... 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    </div> 

    <!-- first part of the second journey from Pontypridd to Llangollen --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ColumnOne'> 
      <input type='radio' name='out' value='2'> 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    <div> 

    <!-- second part of the second journey from Pontypridd to Llangollen --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ConnectingJournies'> 
      <p>...</p> 
     </div> 
     <div class='ColumnOne'> 
      ... doesn't contain a radio input ... 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    </div> 

    ... some more outbound journey options ... 

    <p>...</p> 
    <h2 id='DirectionHeader'>Inbound Options</h2> 
    <p>Llangollen to Pontypridd, 07/11/1910</p> 

    <!-- first part of the first journey from Llangollen to Pontypridd --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ColumnOne'> 
      <input type='radio' checked='checked' name='in' value='1'> 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    <div> 

    <!-- second part of the first journey from Llangollen to Pontypridd --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ConnectingJournies'> 
      <p>...</p> 
     </div> 
     <div class='ColumnOne'> 
      ... doesn't contain a radio input ... 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    </div> 

    <!-- first part of the second journey from Llangollen to Pontypridd --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ColumnOne'> 
      <input type='radio' name='in' value='2'> 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    <div> 

    <!-- second part of the second journey from Llangollen to Pontypridd --> 
    <div class='JourneyOptions'> 
    <div class='Journey'> 
     <div class='ConnectingJournies'> 
      <p>...</p> 
     </div> 
     <div class='ColumnOne'> 
      ... doesn't contain a radio input ... 
     </div> 
     ... some more divs of parseable journey info ... 
    </div> 
    </div> 

    ... some more inbound journey options ... 
</div> 

很抱歉的大例子,但我認爲這是小我可以把它同時仍然代表我的問題。

回答

0

節點集恰好......集:主機語言依賴性排序(最多的文檔順序)唯一節點。如果你想要表達某種層次或分組的結果,答案是你不能。

所以,你可以選擇各組與啓動:當時

/div[@class='TableHolder'] 
    /div[@class='JourneyOptions'] 
     [div[@class='Journey'] 
      /div[@class='ColumnOne'] 
       /input[@type='radio'] 
     ] 

一組(有很多選擇):

/div[@class='TableHolder'] 
    /div[@class='JourneyOptions'] 
     [count(
      (self::div|preceding-sibling::div) 
       [div[@class='Journey'] 
        /div[@class='ColumnOne'] 
         /input[@type='radio'] 
       ] 
      ) = 1 
     ]