I have a xml file which I need to retrieve the node "DocIDAutoNumerator" value but I only need to take it if the filed "ActivityTime" contains the date of today (2021-08-11) lets say and if the node "DocumentTypeValue" equals 1319. I was trying for hours but can mange to retrieve it with what I did. This is what I did -
XPath
//Document[.//Field[Code[text()='DocumentTypeValue'] and Value[text()='1319']] and //ActivityTime[contains(text(),'2021-08-11')] ]//Fields[Field[Code="DocumentTypeValue"]] /Field[Code="DocIDAutoNumerator"]/Value
XML
<root>
<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>
</root>
CodePudding user response:
This XPath,
/root/Document[normalize-space(ActivityTime)='2021-08-11']
//Fields[Field/Code='DocumentTypeValue']
/Field[Code='DocIDAutoNumerator']/Value/text()
selects the text of the targeted Value
,
123121
as requested.
Explanation
Select
Document
based on the givenActivityTime
value.- Use
normalize-space()
to eliminate variances to do, in this case, trailing whitespace.
- Use
From there, select
Fields
based on the givenField/Code
value.From there, select the
Field based on the given
Code` value.Select that
Field
element'sValue
child's text.
Related
Update:
To also add that the Field
with a DocumentTypeValue
Code
must have a 4008
Value
:
/root/Document[normalize-space(ActivityTime)='2021-08-11']
//Fields[Field[Code='DocumentTypeValue'][Value='4008']]
/Field[Code='DocIDAutoNumerator']/Value/text()
CodePudding user response:
This (admittedly convoluted) xpath expression
//Document[contains(.//ActivityTime,"2021-08-11")] \
[.//Field[.//Code[.="DocumentTypeValue"]][.//Value[.="4008"]]] \
//Field[./Code[.="DocIDAutoNumerator"]]/Value
should output, given your example xml
123121