如何使用Groovy

时间:2015-05-06 13:23:17

标签: xpath groovy

我想知道是否可以使用Xpath从单个Xpath查询返回mutliple元素而不使用union'|'操作

我遇到的问题是,我希望在以下XML的一个Xpath查询中返回两个位于XML结构不同级别的元素(employment_information.person_id_external和job_information.start_date):

<CompoundEmployee>
  <person>
    <person_id_external>21554</person_id_external>
    <employment_information>
      <start_date>2014-02-27</start_date>
      <job_information><end_date>2013-04-21</end_date><event>H</event><start_date>2012-09-28</start_date></job_information>
      <job_information><end_date>2013-04-26</end_date><event>5</event><start_date>2013-04-22</start_date></job_information>
      <job_information><end_date>9999-12-31</end_date><event>R</event><start_date>2014-02-27</start_date></job_information>
    </employment_information>
  </person>
  <person>
    <person_id_external>8265</person_id_external>
    <employment_information>
      <start_date>2000-10-02</start_date>
      <job_information><end_date>2014-10-24</end_date><event>5</event><start_date>2014-05-22</start_date></job_information>
      <job_information><end_date>2014-05-21</end_date><event>H</event><start_date>2000-10-02</start_date></job_information>
      <job_information><end_date>9999-12-31</end_date><event>5</event><start_date>2014-10-25</start_date></job_information>
    </employment_information>
  </person>
  <execution_timestamp>2015-05-05T08:17:51.000Z</execution_timestamp>
  <version_id>1502P0</version_id>
</CompoundEmployee>

我到目前为止写的XPath查询是:

XPath x = XPath.newInstance("/*/*/*/job_information[number(translate(start_date,'-','')) < number(translate(../start_date,'-','')) and (event = 'H' or event = 'R')]/start_date");

Employees = x.selectNodes(doc);

for (Element Employee : Employees) {

    fieldName = Employee.getName();
    fieldValue = Employee.getText();

    println ("Job Information Selection ${fieldName} = ${fieldValue}");
}

有效但只返回job_information.start_date。如果我联合相同的查询但返回employment_information.person_id_external,则查询返回两行 - 每个联合1行,但我希望它们返回到同一行但是作为两个字段,如果这有意义的话。我可以这样做吗?如果是这样,我该怎么做?

谢谢,

格雷格

0 个答案:

没有答案