我有一個需要處理的 xml 檔案(將其添加到帖子的末尾)。只有當我找到節點“DocumentTypeValue”=1319 時,我才需要檢索節點“DocIDAu??toNumerator”。有沒有辦法只在一個 XPATH 中查看它?或者我應該查找 DocumentTypeValue ==1319 并獲取當前位置,以便我可以使用剛剛檢索到的位置創建 XPATH?謝謝大家的幫助!
該檔案是 -
<Document>
<Labels>
<Label>
<Fields>
<Field>
<Code>DocumentTypeValue</Code>
<Value>4008</Value>
</Field>
<Field>
<Code>DocIDAutoNumerator</Code>
<Value>123121</Value>
</Field>
</Fields>
</Label>
</Labels>
<ActivityTime>2021-08-11 </ActivityTime>
</Document>
<Document>
<Labels>
<Label>
<Fields>
<Field>
<Code>DocumentTypeValue</Code>
<Value>1319</Value>
</Field>
<Field>
<Code>DocIDAutoNumerator</Code>
<Value>21321</Value>
</Field>
</Fields>
</Label>
</Labels>
</Document>
uj5u.com熱心網友回復:
這個 XPath,
//Fields[Field[Code="DocumentTypeValue"][Value="1319"]]
/Field[Code="DocIDAutoNumerator"]/Value
選擇Value一個的元件Field,其
- 有一個
"DocIDAutoNumerator"Code,并且 Fields與 a位于同一元素Field中- 有一個
"DocumentTypeValue"Code并且有一個"1319"Value。
- 有一個
uj5u.com熱心網友回復:
要獲取<Field>包含<Code>值為 "DocIDAu??toNumerator" 且具有Field/Code值為 "1319"的同級元素的節點,您可以使用以下 XPath-1.0 運算式:
//Field[Code='DocIDAutoNumerator' and ../Field[Value='1319']]
這將回傳<Field>具有所需屬性的元素。
要獲取它的Value,請將字串附加/Value到運算式中。
要檢索其Code元素,請附加 String /Code。
uj5u.com熱心網友回復:
這應該有效:
//Document[.//Code/text() = "DocumentTypeValue"/following-sibling::Value/text() = "1319"]////Code/text() = "DocIDAutoNumerator"/following-sibling::Value/text()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363086.html
上一篇:Python/BeautifulSoup-從clinicaltrials.gov中提取XML資料,只能提取沒有缺失資料的研究
