用 XPath 替换元素

Replace element by XPath

我尝试在 Citrus Framework 中替换输入 XML 的一些元素。

我的 Spring 上下文包含:

  <citrus:namespace-context>
    <citrus:namespace prefix="def" uri="http://sample.com/xmlns/2005"/>
  </citrus:namespace-context>

我的输入文件以:

开头
<?xml version="1.0" encoding="UTF-8"?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <ns0:canonicalMessageHeader xmlns:ns0="http://sample.com/xmlns/2005">
            <ns0:headerVersion>1.0</ns0:headerVersion>
            <ns0:bodyVersion>1.0</ns0:bodyVersion>
            <ns0:trackingInfo>
                <ns0:eventHandlerInitInfo>
                    <ns0:processInfo>
                        <ns0:adapterTrackingId>214F27DF-E1FB-4E84-9122-390C5876ABD2:1</ns0:adapterTrackingId>
                        ...

我的端点是这样配置的:

<send endpoint="jms:topic:Order.Request?timeout=10000&amp;connectionFactory=DEVconnectionFactoryFrom">
                        <message>
                            <resource file="com/sample/citrus/messages/input/SalesOrderTo.xml"/>
                            <element value="${track}" path="SOAP-ENV:Envelope/SOAP-ENV:Header/def:canonicalMessageHeader/def:trackingInfo/def:eventHandlerInitInfo/def:processInfo/def:adapterTrackingId"/>

我有以下错误:

Can not evaluate xpath expression 'SOAP-ENV:Envelope/SOAP-ENV:Header/def:canonicalMessageHeader/def:trackingInfo/def:eventHandlerInitInfo/def:processInfo/def:adapterTrackingId'
        at com/sample/citrus/SalesOrderToIT(sequential:45)
        at com/sample/citrus/SalesOrderToIT(send:48-82)
Caused by: javax.xml.xpath.XPathExpressionException: org.apache.xpath.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: def

这个错误的可能原因是什么?

此致

您在 XML 中将 nso 定义为名称空间前缀,然后在 XPath 上使用 def,应该是 nso.

在通过 XPath 覆盖发送操作中的消息元素时,Citrus 中缺少全局命名空间声明支持。已跟踪此问题:https://github.com/christophd/citrus/issues/331

同时,您必须使用与消息模板文件中完全相同的命名空间前缀 - 在您的情况下 ns0:

您也可以放弃 XPath 覆盖并使用点标记的节点覆盖,如下所示:

<send endpoint="jms:topic:Order.Request?timeout=10000&amp;connectionFactory=DEVconnectionFactoryFrom">
    <message>
        <resource file="com/sample/citrus/messages/input/SalesOrderTo.xml"/>
        <element value="${track}" path="Envelope.Header.canonicalMessageHeader.trackingInfo.eventHandlerInitInfo.processInfo.adapterTrackingId"/>
    </message>
</send>

点表示法不基于名称空间,而是使用本地元素名称在消息模板中查找元素。显然不如 XPath 强大,但它适用于当前版本的框架。