我有一個需要處理的 xml 檔案(將其添加到帖子的末尾)。只有當我找到節點“DocumentTypeValue”=1319 時,我才需要檢索節點“ActivityTime”。有沒有辦法只在一個 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>
<ActivityTime>1993-08-11 </ActivityTime>
</Document>
uj5u.com熱心網友回復:
首先,您需要將 Document 節點包裝在單個父節點中,因為 XML 檔案只能有一個根。
然后您可以使用以下 XPath 選擇器:
//Document[.//Field[Code[text()='DocumentTypeValue'] and Value[text()='1319']]]//ActivityTime
翻譯:查找具有型別后代的型別的任何后代,Document該后代Field包含Code具有文本“DocumentTypeValue”的Value子代和具有文本“1319”的子代。然后獲取Document型別為 ActivityTime的該節點的后代。
此類 XPath 運算式的一般結構為:
//ParentNode[expressions to narrow down which parent node I want]//NodeIAmLookingFor[if necessary, expression to narrow down which node I am looking for]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363069.html
上一篇:使用saxon決議xsl檔案后,如何將XSL檔案中的值設為字串以及如何洗掉<dot-filename>graphs/node</dot-filename>標記?
