【问题标题】:xpath filter - how to filter to the latest node recordxpath 过滤器 - 如何过滤到最新的节点记录
【发布时间】:2020-01-23 09:18:45
【问题描述】:

我在过滤指定节点上的 xpath 以仅包含最新记录时遇到问题。在示例 xml 中有一条规则,即每个节点上的第一条记录是最新信息。我想过滤所有不相关的记录(不同于第一条记录)。

第二条规则是我不想使用日期条件过滤第一个之后的所有 job_information 记录。

通常我使用带有如下表达式的 xpath 测试器: /queryCompoundEmployeeResponse/CompoundEmployee[(person/employment_information/job_information[1])] 这只给了我工作信息的第一条记录,但这里是行不通的。你能告诉我它有什么问题吗?

你能帮帮我吗?

带有 3 个工作信息记录的 xml 输入

<queryCompoundEmployeeResponse>
  <CompoundEmployee>
    <id>11111</id>
    <person>
      <employment_information>
        <job_information>
          <end_date>9999-12-31</end_date>
          <start_date>2017-05-17</start_date>
        </job_information>
        <job_information>
          <end_date>2018-12-31</end_date>
          <start_date>2017-05-17</start_date>
        </job_information>
        <job_information>
          <end_date>2016-12-31</end_date>
          <start_date>2013-05-17</start_date>
        </job_information>
      </employment_information>
    </person>
  </CompoundEmployee>
</queryCompoundEmployeeResponse>

我想要的xml输出

<queryCompoundEmployeeResponse>
  <CompoundEmployee>
    <id>11111</id>
    <person>
      <employment_information>
        <job_information>
          <end_date>9999-12-31</end_date>
          <start_date>2017-05-17</start_date>
        </job_information>
      </employment_information>
    </person>
  </CompoundEmployee>
</queryCompoundEmployeeResponse>

【问题讨论】:

    标签: html xml xpath


    【解决方案1】:

    假设您始终拥有相同的 XML 结构,您可以尝试:

    data=xmlParse("C:/Users/.../pathtoyourxmlfile.xml")
    a=xpathSApply(data,"count(//job_information[1]/ancestor::*)+6")
    b=xpathSApply(data,"count(//job_information)-1")*4+(a-1)
    
    old=read_lines("C:/Users/.../pathtoyourxmlfile.xml")
    new = old[-(a:b)]
    
    writeLines(new,con = "new.xml")
    

    输出(new.xml):

    <queryCompoundEmployeeResponse>
      <CompoundEmployee>
        <id>11111</id>
        <person>
          <employment_information>
            <job_information>
              <end_date>9999-12-31</end_date>
              <start_date>2017-05-17</start_date>
            </job_information>
          </employment_information>
        </person>
      </CompoundEmployee>
    </queryCompoundEmployeeResponse>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2018-03-03
      • 1970-01-01
      相关资源
      最近更新 更多