【问题标题】:XPath with multiple conditions at multiple levels?XPath 在多个级别具有多个条件?
【发布时间】:2022-01-03 02:15:32
【问题描述】:

我有一个 xml 文件,我需要检索节点“DocIDAu​​toNumerator”值,但如果提交的“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>

【问题讨论】:

    标签: xml xpath


    【解决方案1】:

    这个 XPath,

    /root/Document[normalize-space(ActivityTime)='2021-08-11']
         //Fields[Field/Code='DocumentTypeValue']
          /Field[Code='DocIDAutoNumerator']/Value/text()
    

    选择目标Value的文本,

    123121
    

    根据要求。

    说明

    1. 根据给定的ActivityTime 值选择Document

      • 使用normalize-space() 消除差异,在本例中为尾随空格。
    2. 从那里,根据给定的Field/Code 值选择Fields

    3. 从那里,选择 Field based on the given Code` 值。

    4. 选择 Field 元素的 Value 子文本。

    相关


    更新:

    还要补充一点,FieldDocumentTypeValue Code 必须有 4008 Value

    /root/Document[normalize-space(ActivityTime)='2021-08-11']
            //Fields[Field[Code='DocumentTypeValue'][Value='4008']]
             /Field[Code='DocIDAutoNumerator']/Value/text()
    

    【讨论】:

    • 感谢您的快速回复!但我还需要将“DocumentTypeValue”等于 1319 条件添加到 XPATH。
    • @FM_Bendo:你确定吗?在您的示例中,没有Document 同时具有1319DocumentTypeValue2021-08-1ActivityTime
    • @FM_Bendo:我已经更新了答案以显示如何按照您的要求进行操作,但您的示例中存在 DocumentTypeValue4008。根据这种模式,您应该能够根据需要适应其他变化。
    • 谢谢你成功了!你有一个很好的网站来学习更多关于 XPATH 的知识吗?
    • 不客气。 Michael Kay 的 XSLT 书很好地教授了 XPath。此外,W3C XPath 1.0 specification 的可读性非常好。
    【解决方案2】:

    这个(诚然令人费解的)xpath 表达式

    //Document[contains(.//ActivityTime,"2021-08-11")]  \
    [.//Field[.//Code[.="DocumentTypeValue"]][.//Value[.="4008"]]]   \
    //Field[./Code[.="DocIDAutoNumerator"]]/Value
    

    应该输出,给定您的示例 xml

    123121
    

    【讨论】:

    • 这也有效 (+1),但可以在某些地方简化:[./e ...] 可以写为 [e ...]f[e[.='val']]f[e='val']
    • 感谢您的回复!当我在一行中运行它时它不起作用......
    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 2019-02-02
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多