具有多个级别的多个条件的 XPath?
XPath with multiple conditions at multiple levels?
我有一个 xml 文件,我需要检索节点“DocIDAutoNumerator”值,但我只需要在提交的“ActivityTime”包含今天的日期 (2021-08-11) 时获取它假设节点“DocumentTypeValue”是否等于 1319。
我试了几个小时,但可以用我所做的设法找回它。
这就是我所做的 -
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>
这个(公认的)xpath 表达式
//Document[contains(.//ActivityTime,"2021-08-11")] \
[.//Field[.//Code[.="DocumentTypeValue"]][.//Value[.="4008"]]] \
//Field[./Code[.="DocIDAutoNumerator"]]/Value
应该输出,给定你的例子xml
123121
这个 XPath,
/root/Document[normalize-space(ActivityTime)='2021-08-11']
//Fields[Field/Code='DocumentTypeValue']
/Field[Code='DocIDAutoNumerator']/Value/text()
selects 目标文本 Value
,
123121
根据要求。
说明
Select Document
基于给定的 ActivityTime
值。
- 使用
normalize-space()
消除差异,在这种情况下,尾随空格。
从那里,select Fields
基于给定的 Field/Code
值。
从那里,select Field based on the given
Code` 值。
Select Field
元素的 Value
子文本。
相关
更新:
还要补充一点,Field
有 DocumentTypeValue
Code
必须有 4008
Value
:
/root/Document[normalize-space(ActivityTime)='2021-08-11']
//Fields[Field[Code='DocumentTypeValue'][Value='4008']]
/Field[Code='DocIDAutoNumerator']/Value/text()
我有一个 xml 文件,我需要检索节点“DocIDAutoNumerator”值,但我只需要在提交的“ActivityTime”包含今天的日期 (2021-08-11) 时获取它假设节点“DocumentTypeValue”是否等于 1319。 我试了几个小时,但可以用我所做的设法找回它。 这就是我所做的 -
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>
这个(公认的)xpath 表达式
//Document[contains(.//ActivityTime,"2021-08-11")] \
[.//Field[.//Code[.="DocumentTypeValue"]][.//Value[.="4008"]]] \
//Field[./Code[.="DocIDAutoNumerator"]]/Value
应该输出,给定你的例子xml
123121
这个 XPath,
/root/Document[normalize-space(ActivityTime)='2021-08-11']
//Fields[Field/Code='DocumentTypeValue']
/Field[Code='DocIDAutoNumerator']/Value/text()
selects 目标文本 Value
,
123121
根据要求。
说明
Select
Document
基于给定的ActivityTime
值。- 使用
normalize-space()
消除差异,在这种情况下,尾随空格。
- 使用
从那里,select
Fields
基于给定的Field/Code
值。从那里,select
Field based on the given
Code` 值。Select
Field
元素的Value
子文本。
相关
更新:
还要补充一点,Field
有 DocumentTypeValue
Code
必须有 4008
Value
:
/root/Document[normalize-space(ActivityTime)='2021-08-11']
//Fields[Field[Code='DocumentTypeValue'][Value='4008']]
/Field[Code='DocIDAutoNumerator']/Value/text()