使用R

时间:2017-09-18 20:04:38

标签: r xml xpath

我有xml文档,它具有以下结构,我想根据Balance属性bsdate值和BalanceRow属性rowNum值选择valueStart和valueEnd属性值。例如:

  • bsdate ='2013'和rowNum ='200'valueStart应为'3000'且valueEnd - '4000'

  • bsdate ='2014'和rowNum ='102'valueStart应为'5500'且valueEnd - '6500'

是否可以在R中进行?我整天都在寻找答案,但却找不到答案

    <Root
    xmlns:a="http://www.w3.org/TR/html4/">
    <Balance bsdate = '2013' bsregdate = '2014.04.01'>
        <BalanceRows type = 'B' rowNum = '100' valueStart = '1000' valueEnd = '2000'></BalanceRows>
        <BalanceRows type = 'B' rowNum = '101' valueStart = '3000' valueEnd = '4000'></BalanceRows>
        <BalanceRows type = 'B' rowNum = '102' valueStart = '5000' valueEnd = '6000'></BalanceRows>
        <BalanceRows type = 'P' rowNum = '200' valueStart = '7000' valueEnd = '8000'></BalanceRows>
        <BalanceRows type = 'P' rowNum = '201' valueStart = '9000' valueEnd = '10000'></BalanceRows>
    </Balance>
    <Balance bsdate = '2014' bsregdate = '2015.04.02'>
        <BalanceRows type = 'B' rowNum = '100' valueStart = '1500' valueEnd = '2500' ></BalanceRows>
        <BalanceRows type = 'B' rowNum = '101' valueStart = '3500' valueEnd = '4500'></BalanceRows>
        <BalanceRows type = 'B' rowNum = '102' valueStart = '5500' valueEnd = '6500'></BalanceRows>
        <BalanceRows type = 'P' rowNum = '200' valueStart = '7500' valueEnd = '8500'></BalanceRows>
        <BalanceRows type = 'P' rowNum = '201' valueStart = '9500' valueEnd = '15000'></BalanceRows>
    </Balance>
</Root>

1 个答案:

答案 0 :(得分:0)

使用测试数据

<input type="range" min=100 max=500 value=100 oninput="setSize(this.value)" onchange="setSize(this.value)">
<div id="container">
  <div class="item"></div>
  <div class="item item-grow">
    <div class="center-wrapper">
      <div class="scroll">
        <div id="too-big"></div>
      </div>
    </div>
  </div>
  <div class="item"></div>
</div>

您可以使用

获取匹配行的属性
library(xml2)
xx <- read_xml("<Root xmlns:a=\"http://www.w3.org/TR/html4/\">
  <Balance bsdate = '2013' bsregdate = '2014.04.01'>
  <BalanceRows type = 'B' rowNum = '100' valueStart = '1000' valueEnd = '2000'></BalanceRows>
  <BalanceRows type = 'B' rowNum = '101' valueStart = '3000' valueEnd = '4000'></BalanceRows>
  <BalanceRows type = 'B' rowNum = '102' valueStart = '5000' valueEnd = '6000'></BalanceRows>
  <BalanceRows type = 'P' rowNum = '200' valueStart = '7000' valueEnd = '8000'></BalanceRows>
  <BalanceRows type = 'P' rowNum = '201' valueStart = '9000' valueEnd = '10000'></BalanceRows>
  </Balance>
  <Balance bsdate = '2014' bsregdate = '2015.04.02'>
  <BalanceRows type = 'B' rowNum = '100' valueStart = '1500' valueEnd = '2500' ></BalanceRows>
  <BalanceRows type = 'B' rowNum = '101' valueStart = '3500' valueEnd = '4500'></BalanceRows>
  <BalanceRows type = 'B' rowNum = '102' valueStart = '5500' valueEnd = '6500'></BalanceRows>
  <BalanceRows type = 'P' rowNum = '200' valueStart = '7500' valueEnd = '8500'></BalanceRows>
  <BalanceRows type = 'P' rowNum = '201' valueStart = '9500' valueEnd = '15000'></BalanceRows>
  </Balance>
  </Root>")