我想從XML文檔(它實際上是一個轉換爲XML的HTML表格)中查找某個元素的最小值。但是,這不符合預期。使用XPath 1.0查找最小值不起作用
查詢結果類似於How can I use XPath to find the minimum value of an attribute in a set of elements?中使用的那個。它看起來像這樣:
/table[@id="search-result-0"]/tbody/tr[
not(substring-before(td[1], " ") > substring-before(../tr/td[1], " "))
]
上執行的示例XML
<table class="tablesorter" id="search-result-0">
<thead>
<tr>
<th class="header headerSortDown">Preis</th>
<th class="header headerSortDown">Zustand</th>
</tr>
</thead>
<tbody>
<tr>
<td width="45px">15 CHF</td>
<td width="175px">Ausgepack und doch nie gebraucht</td>
</tr>
<tr>
<td width="45px">20 CHF</td>
<td width="175px">Ausgepack und doch nie gebraucht</td>
</tr>
<tr>
<td width="45px">25 CHF</td>
<td width="175px">Ausgepack und doch nie gebraucht</td>
</tr>
<tr>
<td width="45px">35 CHF</td>
<td width="175px">Ausgepack und doch nie gebraucht</td>
</tr>
<tr>
<td width="45px">14 CHF</td>
<td width="175px">Gebraucht, aber noch in Ordnung</td>
</tr>
<tr>
<td width="45px">15 CHF</td>
<td width="175px">Gebraucht, aber noch in Ordnung</td>
</tr>
<tr>
<td width="45px">15 CHF</td>
<td width="175px">Gebraucht, aber noch in Ordnung</td>
</tr>
</tbody>
</table>
查詢返回以下結果:
<tr>
<td width="45px">15 CHF</td>
<td width="175px">Ausgepack und doch nie gebraucht</td>
</tr>
-----------------------
<tr>
<td width="45px">14 CHF</td>
<td width="175px">Gebraucht, aber noch in Ordnung</td>
</tr>
-----------------------
<tr>
<td width="45px">15 CHF</td>
<td width="175px">Gebraucht, aber noch in Ordnung</td>
</tr>
-----------------------
<tr>
<td width="45px">15 CHF</td>
<td width="175px">Gebraucht, aber noch in Ordnung</td>
</tr>
爲什麼有更多的節點不是一個回來了?由於只有一個最小值,所以應該只返回一個節點。有人看到查詢有什麼問題嗎?它應該只返回包含14 CHF
的節點。使用http://xpath.online-toolz.com/tools/xpath-editor.php
你是對的。調用節點集上的函數僅返回第一個節點的結果而不是集合。有關如何解決這個問題的任何建議? – str 2014-09-23 10:00:59
@str我很想說這在XPath 1.0中是不可能的。你能預先操作元素嗎?如果'substring-before'可以是在應用XPath表達式之前執行的一個獨立步驟(這樣就剩下了),那麼我有一個解決方案。 – 2014-09-23 10:18:57
我同意Mathias。這*在XPath 1.0中是不可能的,無需更改輸入XML。 – Tomalak 2014-09-23 10:43:07