【问题标题】:Extract the Element Value from the latest Node with xpath使用 xpath 从最新节点中提取元素值
【发布时间】:2022-01-09 15:38:16
【问题描述】:

我有 XML 代码,我需要从中提取最新元素的 ID。

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <JsonData>
        <Dokument_ID>26a914b5</Dokument_ID>
        <Dokument_Art>DIV</Dokument_Art>
        <Document_creation_date>02.12.2021 08:51</Document_creation_date>
        <Document_filesize>4234</Document_filesize>
    </JsonData>
    <JsonData>
        <Dokument_ID>cc88ae09</Dokument_ID>
        <Dokument_Art>DIV</Dokument_Art>
        <Document_creation_date>02.12.2021 08:54</Document_creation_date>
        <Document_filesize>184706</Document_filesize>
    </JsonData>
    <JsonData>
        <Dokument_ID>22f14c7b</Dokument_ID>
        <Dokument_Art>DIV</Dokument_Art>
        <Document_creation_date>02.12.2021 15:09</Document_creation_date>
        <Document_filesize>4234</Document_filesize>
    </JsonData>
</root>

通过以下 xpath,我设法获得具有最大文件大小的元素的 ID 号

/root/JsonData[not(../JsonData/Document_filesize > Document_filesize)]/Dokument_ID

很遗憾,这对于创建日期的工作方式不同。 我的猜测是,这是因为 Docment_creation_data 的内容是一个带有特殊字符串字符的日期。

运行所需 xpath 的选项有哪些?

【问题讨论】:

    标签: xml xpath


    【解决方案1】:

    通过以下 xpath,我设法获得了具有最大文件大小的元素的 ID 号

    /root/JsonData[not(../JsonData/Document_filesize > Document_filesize)]/Dokument_ID
    

    我不知道您正在使用哪个软件(支持 XPath),但这可能也可以:

    root/JsonData[Document_filesize=max(../JsonData/Document_filesize)]/Dokument_ID
    

    不幸的是,这对于创建日期的工作方式不同。我的猜测是,这是因为Document_creation_data 的内容是一个带有特殊字符串字符的日期。

    由于自定义格式,它不是官方日期时间。您必须将其转换为 1 才能进行任何比较。

    以下中间步骤是使用命令行工具 完成的,我一直用于 XPath/XQuery:

    $ xidel -s input.xml -e 'root/JsonData/Document_creation_date'
    02.12.2021 08:51
    02.12.2021 08:54
    02.12.2021 15:09
    
    $ xidel -s input.xml -e '
      root/JsonData/replace(
        Document_creation_date,
        "(\d{2})\.(\d{2})\.(\d{4}) (.+)",
        "$3-$2-$1T$4:00"
      )
    '
    2021-12-02T08:51:00
    2021-12-02T08:54:00
    2021-12-02T15:09:00
    
    $ xidel -s input.xml -e '
      root/max(
        JsonData/dateTime(
          replace(
            Document_creation_date,
            "(\d{2})\.(\d{2})\.(\d{4}) (.+)",
            "$3-$2-$1T$4:00"
          )
        )
      )
    '
    2021-12-02T15:09:00
    
    $ xidel -s input.xml -e '
      root/format-dateTime(
        max(
          JsonData/dateTime(
            replace(
              Document_creation_date,
              "(\d{2})\.(\d{2})\.(\d{4}) (.+)",
              "$3-$2-$1T$4:00"
            )
          )
        ),
        "[D01].[M01].[Y] [H01]:[m01]"
      )
    '
    02.12.2021 15:09
    
    $ xidel -s input.xml -e '
      root/JsonData[
        Document_creation_date = format-dateTime(
          max(
            ../JsonData/dateTime(
              replace(
                Document_creation_date,
                "(\d{2})\.(\d{2})\.(\d{4}) (.+)",
                "$3-$2-$1T$4:00"
              )
            )
          ),
          "[D01].[M01].[Y] [H01]:[m01]"
        )
      ]/Dokument_ID
    '
    22f14c7b
    

    更好的是,使用 XQuery:

    $ xidel -s input.xml -e '
      (
        for $x in //JsonData
        order by dateTime(
          replace(
            $x/Document_creation_date,
            "(\d{2})\.(\d{2})\.(\d{4}) (.+)",
            "$3-$2-$1T$4:00"
          )
        )
        return
        $x/Dokument_ID
      )[last()]
    '
    22f14c7b
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多