如何在空手道框架中获取子元素

如何在空手道框架中获取子元素

本文介绍了如何在空手道框架中获取子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个api的响应中提取子元素,然后将其动态传递给下一个api请求.

I need to extract child elements from response of one api and then pass it dynamically to next api request.

假设我具有以下xml:

Suppose i have the below xml:

* def foo =
"""
<records>
  <record index="1">a</record>
  <record index="2">b</record>
  <record index="3" foo="bar">c</record>
</records>
"""

我只想提取这个:

  <record index="1">a</record>
  <record index="2">b</record>
  <record index="3" foo="bar">c</record>

我尝试了以下选项,但没有一个起作用:

I tried below options, but none of then worked:

* def chk = foo//records/*
* def chk = foo//records/child::*
* def chk = foo//records/descendant::*
* print chk

打印后,我得到以下信息,如果我缺少任何东西或其他任何方式,请提出建议.谢谢!

After printing, I get the below, please suggest if i'm missing anything or any other way to do it.Thank you!

13:51:07.046 INFO  - [print] {
  "records": {
    "record": [
      {
        "_": "a",
        "@": {
          "index": "1"
        }
      },
      {
        "_": "b",
        "@": {
          "index": "2"
        }
      },
      {
        "_": "c",
        "@": {
          "foo": "bar",
          "index": "3"
        }
      }
    ]
  }
}

推荐答案

如果您需要做的只是获取一组元素并将它们填充到另一个XML模板中,则看起来像是简单的字符串替换即可解决问题:(我将原来较长的答案留在该答案的后面,以供参考)

looks like a simple string replace would do the trick if all you need to do is take a set of elements and stuff them into another XML template:(I'm leaving the original longer answer at the end of this answer for reference)

* def foo =
"""
<records>
  <record index="1">a</record>
  <record index="2">b</record>
  <record index="3" foo="bar">c</record>
</records>
"""
* replace foo.<records> = ''
* replace foo.</records> = ''
# you can do def bar = read('template.txt') in your case
* text bar =
"""
<after new="namespace">
  @@foo@@
</after>
"""
* replace bar.@@foo@@ = foo
* xml bar = bar
* match bar ==
"""
<after new="namespace">
  <record index="1">a</record>
  <record index="2">b</record>
  <record index="3" foo="bar">c</record>
</after>
"""

是的,空手道本身不支持XML节点列表,通常不需要. XML具有这种特殊性,您总是需要一个父"或包装"元素.

Yes, Karate does not support an XML node-list natively, typically you don't need it. And XML has this peculiarity where you always need a "parent" or "wrapping" element.

如上所示,您确实拥有所需的所有信息.我仍然不清楚您需要将哪种XML传递给下一个API-但是无论如何,空手道中都有一种方法.这是2个选项.首先,遍历数据并手动发出XML:

As you see above, you do have all the information needed. I'm still not clear what kind of XML you need to pass to the next API - but whatever the case, there is a way in Karate. Here are 2 options. First, iterating over the data and manually emitting XML:

* def foo =
"""
<records>
  <record index="1">a</record>
  <record index="2">b</record>
  <record index="3" foo="bar">c</record>
</records>
"""

* json records = foo.records.record
* def temp = <bar></bar>
* def fun =
"""
function(r, i){
  karate.set('temp', '/bar/record[' + (i+1) + ']', r._);
  karate.set('temp', '/bar/record[' + (i+1) + ']/@index', r['@'].index);
}
"""
* eval karate.forEach(records, fun)
* match temp ==
"""
<bar>
  <record index="1">a</record>
  <record index="2">b</record>
  <record index="3">c</record>
</bar>
"""

请注意,karate.forEach仅在即将到来的0.8.0中可用,但是您可以尝试使用0.8.0.RC3

Note that karate.forEach is available only in the upcoming 0.8.0 but you can try 0.8.0.RC3

这是另一个有趣的选项,如果您需要做的就是更改XML父级,则可以使用字符串替换来解决问题:

Here's another interesting option, if all you need to do is change the XML parent, a string replace will do the trick:

* replace foo.<records> = '<bar>'
* replace foo.</records> = '</bar>'
* xml bar = foo
* match bar ==
"""
<bar>
  <record index="1">a</record>
  <record index="2">b</record>
  <record index="3" foo="bar">c</record>
</bar>
"""

这篇关于如何在空手道框架中获取子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:30