我正在尝试将SOAP请求XML中的一些值复制到SOAP响应XML文件中。
请考虑以下请求和响应XML以供参考:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ehit="http://www.calheers.ca.gov/EHITSAWSInterfaceMessageSchema" xmlns:ehit1="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema" xmlns:ns="http://niem.gov/niem/structures/2.0" xmlns:ns2="http://niem.gov/niem/niem-core/2.0" xmlns:ns1="http://niem.gov/niem/domains/screening/2.1">
<soapenv:Header/>
<soapenv:Body>
<ehit:DeterminationRequest>
<ehit:MessageInformation>
<ehit1:MessageID ns:id="?" ns:metadata="?" ns:linkMetadata="?">? </ehit1:MessageID>
<ehit1:MessageTimeStamp ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:MessageTimeStamp>
<ehit1:SendingSystem ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:SendingSystem>
<ehit1:ReceivingSystem ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:ReceivingSystem>
<ehit1:FipsCountyCode ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:FipsCountyCode>
<!--Optional:-->
<ehit1:TracerID ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:TracerID>
</ehit:MessageInformation>
.......continued......
响应XML是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ehit="http://www.calheers.ca.gov/EHITSAWSInterfaceMessageSchema" xmlns:ehit1="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema" xmlns:ns="http://niem.gov/niem/structures/2.0">
<soapenv:Header/>
<soapenv:Body>
<ehit:DeterminationResponse>
<ehit1:MessageID ns:id=${id} ns:metadata=${metadata} ns:linkMetadata="?">?</ehit1:MessageID>
<ehit1:AckTimeStamp ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:AckTimeStamp>
<!--Optional:-->
<ehit1:StatusCode ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:StatusCode>
<!--Optional:-->
<ehit1:ErrorCode ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:ErrorCode>
<!--Optional:-->
<ehit1:ErrorMessage ns:id="?" ns:metadata="?" ns:linkMetadata="?">?</ehit1:ErrorMessage>
</ehit:DeterminationResponse>
</soapenv:Body>
</soapenv:Envelope>
我在这里创建了两个动态变量$ {id}和$ {metadata}。
我尝试复制.Now的id和元数据属性,将这些值从请求设置为响应我正在使用以下脚本:
def req = new XmlSlurper().parseText(mockRequest.requestContent)
requestContext.id = req.Body.DeterminationRequest.MessageInformation.MessageID.@id;
requestContext.metadata = req.Body.DeterminationRequest.MessageInformation.MessageID.@metadata;
但它没有返回任何价值。有人可以帮我弄清楚我在这里做错了吗?
另外,如果我想将许多(考虑50+)值从请求复制到响应中,那么有没有简单的方法而不是创建50多个变量?因为在我的情况下,我必须将所有值从请求复制到响应消息。
答案 0 :(得分:0)
我认为问题在于,您并未真正获得id
和metadata
属性的价值,因为它来自http://niem.gov/niem/structures/2.0
命名空间ns:id
和{ {1}}。请参阅this related question on SO。
因此,请尝试记录您的表达式:
ns:metadata
它将返回空白。这可能是您在模拟响应中看不到预期值的原因。
解决方案是使用命名空间来获取属性值,更改代码,例如:
log.info req.Body.DeterminationRequest.MessageInformation.MessageID.@id
每次完整路径时都不能写入更容易维护:
import groovy.xml.Namespace
// define namespace
def ns = new Namespace('http://niem.gov/niem/structures/2.0', 'ns')
def req = new XmlSlurper(false,true).parseText(mockRequest.requestContent)
context.id = req.Body.DeterminationRequest.MessageInformation.MessageID[0].attributes()[ns.id.toString()]
context.metadata = req.Body.DeterminationRequest.MessageInformation.MessageID[0].attributes()[ns.metadata.toString()]