【问题标题】:How to use XPATH functions in Mule如何在 Mule 中使用 XPATH 函数
【发布时间】:2015-01-13 10:03:30
【问题描述】:

我有一个输入 SOAP XML 如下:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.com/schema/MainData/V1">
   <soapenv:Header>
    </soapenv:Header>
   <soapenv:Body>
      <v1:retrieveDataRequest>
         <v1:Id>662</v1:Id>
      </v1:retrieveDataRequest>
   </soapenv:Body>
</soapenv:Envelope>

现在我的问题很简单......

如何在 Mule 中使用 XPATH 函数,如 count()、name()、substring() 等

我想在这个 SOAP 请求上使用 XPATH,如下所示:-

 <logger message="#[xpath('count(//v1:retrieveDataRequest/v1:Id)')]" level="INFO" doc:name="Logger"/>

但是它抛出了以下异常:-

Exception stack is:
1. No Such Function {http://www.mulesoft.org/schema/mule/core}:count (org.jaxen.UnresolvableException)
  org.jaxen.SimpleFunctionContext:127 (null)
2. Failed to evaluate XPath expression: "count(//v1:retrieveDataRequest/v1:Id)" (org.mule.api.MuleRuntimeException)
  org.mule.module.xml.expression.AbstractXPathExpressionEvaluator:141 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MuleRuntimeException.html)
3. org.mule.api.MuleRuntimeException: Failed to evaluate XPath expression: "count(//v1:retrieveDataRequest/v1:Id)" (java.lang.RuntimeException)
  org.mule.module.xml.el.XPathFunction:70 (null)
4. [Error: org.mule.api.MuleRuntimeException: Failed to evaluate XPath expression: "count(//v1:retrieveDataRequest/v1:Id)"]
[Near : {... xpath('count(//v1:retrieveData ....}]
             ^
[Line: 1, Column: 1] (org.mule.mvel2.CompileException)
  org.mule.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer:437 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/mvel2/CompileException.html)
5. Execution of the expression "xpath('count(//v1:retrieveDataRequest/v1:Id)')" failed. (org.mule.api.expression.ExpressionRuntimeException)
  org.mule.el.mvel.MVELExpressionLanguage:202 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/expression/ExpressionRuntimeException.html)
6. Execution of the expression "xpath('count(//v1:retrieveDataRequest/v1:Id)')" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: String (org.mule.api.MessagingException)
  org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:32 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.jaxen.UnresolvableException: No Such Function {http://www.mulesoft.org/schema/mule/core}:count
    at org.jaxen.SimpleFunctionContext.getFunction(SimpleFunctionContext.java:127)
    at org.jaxen.ContextSupport.getFunction(ContextSupport.java:242)
    at org.jaxen.Context.getFunction(Context.java:216)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

请注意 :- 我已经使用了 Mule namespace-manager 如下:-

<mulexml:namespace-manager includeConfigNamespaces="true">
    <mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
    <mulexml:namespace prefix="v1" uri="http://services.test.com/schema/MainData/V1"/>
</mulexml:namespace-manager>

我得到以下结果:- &lt;logger message="#[xpath('//v1:retrieveDataRequest/v1:Id').text]" level="INFO" doc:name="Logger"/&gt;

请帮忙

【问题讨论】:

    标签: xml soap xpath mule mule-studio


    【解决方案1】:

    您没有在上面显示的异常的相关位是:

    根异常堆栈跟踪: org.jaxen.UnresolvableException: 没有这样的功能 {http://www.mulesoft.org/schema/mule/core}:count 在 org.jaxen.SimpleFunctionContext.getFunction(SimpleFunctionContext.java:127) 在 org.jaxen.ContextSupport.getFunction(ContextSupport.java:242) 在 org.jaxen.Context.getFunction(Context.java:216) + 3 个以上(设置调试级别日志记录或“-Dmule.verbose.exceptions=true”为所有内容)

    如您所见,count XPath 函数在 Mule 命名空间中进行评估。这是在您的namespace-manager 中使用includeConfigNamespaces="true" 的副作用。您似乎不需要 Mule 名称空间来评估您的 XPath(您没有在 SOAP 消息中使用任何 Mule 名称空间),因此您应该将此值设置为 false。

    这将在 Mule 3.6 中修复,每个 MULE-7030

    【讨论】:

    • 太棒了......谢谢大卫......我已经更新了这个问题,但有例外
    【解决方案2】:

    所以,根据大卫的建议.. 最终的工作解决方案是设置 includeConfigNamespaces="false" :-

    <mulexml:namespace-manager includeConfigNamespaces="false">
        <mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
        <mulexml:namespace prefix="v1" uri="http://services.test.com/schema/MainData/V1"/>
    </mulexml:namespace-manager>
    

    所以现在,我可以毫无问题地使用像count() 这样的XPATH 函数了:-

    <logger message="#[xpath('count(//v1:retrieveDataRequest/v1:Id)')]" level="INFO" doc:name="Logger"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2014-01-26
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多