编辑:发布很长的答案后(请参阅此答案的结尾/第二部分),我意识到有一个超级简单的解决方案供您使用replace https://github.com/intuit/karate#replace空手道的语法:
* def integrationName = 'hello'
* def tenantID = 'world'
* def json =
"""
{
"integration": {
"serviceData": {
"integrationService": {
"name": "#(integrationName)",
"description": "#(tenantID)",
"serviceData": "<xml xmlns=\"http://www.w3.org/1999/xhtml\">\n <block type=\"start_block\" id=\"foOIiCF5aZGnie1GzBDB\" deletable=\"false\" x=\"150\" y=\"25\">\n <field name=\"ORCH_NAME\">Add2NumDocInputs</field>\n <field name=\"SVC_SIGNATURE\">{\"sig_in\":{\"rec_ref\":\"fld_[<tenantID>]_stage00.documenttypes:sum2\",\"field_type\":\"recref\",\"node_type\":\"record\",\"field_dim\":\"0\"},\"sig_out\":{\"field_type\":\"record\",\"node_type\":\"record\",\"field_dim\":\"0\"}}</field>\n <field name=\"AUDIT_SETTINGS\"></field>\n <statement name=\"ADD_BLOCKS\">\n <block type=\"service_block_math\" id=\"aUqb0MAozTHQFuHj5rma\">\n <field name=\"SERVICE\">pub.math:addInts</field>\n <field name=\"SERVICE_DESC_FIELD\"></field>\n <field name=\"SERVICE_DETAILS\">{\"service\":\"pub.math:addInts\",\"map\":[{\"sourcePath\":\"/n2;1;0\",\"targetPath\":\"/num2;1;0\"},{\"sourcePath\":\"/n1;1;0\",\"targetPath\":\"/num1;1;0\"}]}</field>\n </block>\n </statement>\n </block>\n</xml>"
}
}
}
}
"""
* replace json.tenantID = tenantID
* match json ==
"""
{
"integration": {
"serviceData": {
"integrationService": {
"name": "hello",
"description": "world",
"serviceData": "<xml xmlns=\"http://www.w3.org/1999/xhtml\">\n <block type=\"start_block\" id=\"foOIiCF5aZGnie1GzBDB\" deletable=\"false\" x=\"150\" y=\"25\">\n <field name=\"ORCH_NAME\">Add2NumDocInputs</field>\n <field name=\"SVC_SIGNATURE\">{\"sig_in\":{\"rec_ref\":\"fld_[world]_stage00.documenttypes:sum2\",\"field_type\":\"recref\",\"node_type\":\"record\",\"field_dim\":\"0\"},\"sig_out\":{\"field_type\":\"record\",\"node_type\":\"record\",\"field_dim\":\"0\"}}</field>\n <field name=\"AUDIT_SETTINGS\"></field>\n <statement name=\"ADD_BLOCKS\">\n <block type=\"service_block_math\" id=\"aUqb0MAozTHQFuHj5rma\">\n <field name=\"SERVICE\">pub.math:addInts</field>\n <field name=\"SERVICE_DESC_FIELD\"></field>\n <field name=\"SERVICE_DETAILS\">{\"service\":\"pub.math:addInts\",\"map\":[{\"sourcePath\":\"/n2;1;0\",\"targetPath\":\"/num2;1;0\"},{\"sourcePath\":\"/n1;1;0\",\"targetPath\":\"/num1;1;0\"}]}</field>\n </block>\n </statement>\n </block>\n</xml>"
}
}
}
}
"""
编辑:这是原始答案(如下),留在这里,因为它可能作为参考有用。
哇,我可以说这是 XML 和 JSON 的可怕组合。嵌入表达式有一个规则,它们必须是字符串start with #(
所以你需要将你的流程分成这样的东西。由于您的有效负载,它很复杂。不是因为空手道:)
* def integrationName = 'hello'
* def tenantID = 'world'
* def recRef = 'fld_[' + tenantID + ']_stage00.documenttypes:sum2'
# by the way this is also possible as an embedded expression #('fld_[' + tenantID + ']_stage00.documenttypes:sum2')
* string signature = {"sig_in":{"rec_ref":"#(recRef)","field_type":"recref","node_type":"record","field_dim":"0"},"sig_out":{"field_type":"record","node_type":"record","field_dim":"0"}}
* def xml =
"""
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="start_block" id="foOIiCF5aZGnie1GzBDB" deletable="false" x="150" y="25">
<field name="ORCH_NAME">Add2NumDocInputs</field>
<field name="SVC_SIGNATURE">#(signature)</field>
<field name="AUDIT_SETTINGS"></field>
<statement name="ADD_BLOCKS">
<block type="service_block_math" id="aUqb0MAozTHQFuHj5rma">
<field name="SERVICE">pub.math:addInts</field>
<field name="SERVICE_DESC_FIELD"></field>
<field name="SERVICE_DETAILS">{"service":"pub.math:addInts","map":[{"sourcePath":"/n2;1;0","targetPath":"/num2;1;0"},{"sourcePath":"/n1;1;0","targetPath":"/num1;1;0"}]}</field>
</block>
</statement>
</block>
</xml>
"""
* xmlstring xml = xml
* def json =
"""
{
"integration": {
"serviceData": {
"integrationService": {
"name": "#(integrationName)",
"description": "#(tenantID)",
"serviceData": "#(xml)"
}
}
}
}
"""
* match json ==
"""
{
"integration": {
"serviceData": {
"integrationService": {
"name": "hello",
"description": "world",
"serviceData": "#string"
}
}
}
}
"""
* match json.integration.serviceData.integrationService.serviceData contains '"rec_ref":"fld_[world]_stage00.documenttypes:sum2"'
另请阅读有关类型转换的文档:https://github.com/intuit/karate#type-conversion https://github.com/intuit/karate#type-conversion