【问题标题】:Not able to form xpath using mule expression for given xml无法使用给定 xml 的 mule 表达式形成 xpath
【发布时间】:2015-01-03 00:49:23
【问题描述】:

我正在尝试使用 mule 中的表达式从 xml 读取数据,但出现以下异常:

java.lang.RuntimeException: org.mule.api.MuleRuntimeException: 无法评估 XPath 表达式:"//*[xpath:local-name()="oa:ID"]/text()"

流程如下

<flow name="testauditFlow1" doc:name="testauditFlow1">
        <http:inbound-endpoint exchange-pattern="one-way"
            host="localhost" port="8086" path="test" doc:name="HTTP"/>
            doc:name="DOM to XML" />
        <expression-component doc:name="Expression"><![CDATA[#[xpath('//*[xpath:local-name()="ID"]/text()').text]]]></expression-component>
        <logger level="INFO" doc:name="Logger" />
    </flow>

我使用的xml如下所示

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <_ord:ProcessOrder releaseID="9.0" versionID="7.0.0.0" xmlns:_ord="http://www.test.com/xmlns/test" xmlns:_wcf="http://www.test.com/xmlns/test/9" xmlns:oa="http://www.test.com/xmlns/test/foundation">
         <oa:ApplicationArea xsi:type="_wcf:ApplicationAreaType">
            <oa:CreationDateTime>2012-08-07T13:25:01.337Z</oa:CreationDateTime>
            <oa:ID>1234566</oa:ID>
            <_wcf:BusinessContext/>
         </oa:ApplicationArea>
      </_ord:ProcessOrder>
   </soapenv:Body>
</soapenv:Envelope>

【问题讨论】:

  • 你为什么在local-name()函数上使用xpath:前缀?去掉前缀还能用吗?

标签: xml xpath mule xslt-2.0 esb


【解决方案1】:

好吧,我可以使用 XPATH 提取值...您可以通过 2 种方式进行:-

1) 第一种方法是使用 local-name 而没有 namespace :-

<logger message="Value of id : #[xpath://.[xpath:local-name()='ID']]" level="INFO" doc:name="Logger"/> 

它工作得很好而且很简单,但是 XPATH 是一种旧方法,在 Mule 中可能会被贬值

2) 第二种方法也是最有效的方法是使用 mulexml:namespace-manager 如下:- 首先,在流程之前在 Mule 配置中使用 mulexml:namespace-manager:-

<mulexml:namespace-manager includeConfigNamespaces="false">
    <mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
    <mulexml:namespace prefix="xsi" uri="http://www.w3.org/2001/XMLSchema-instance"/>
    <mulexml:namespace prefix="soapenc" uri="http://schemas.xmlsoap.org/soap/encoding/"/>
    <mulexml:namespace prefix="xsd" uri="http://www.w3.org/2001/XMLSchema"/>
    <mulexml:namespace prefix="_ord" uri="http://www.test.com/xmlns/test"/>
    <mulexml:namespace prefix="_wcf" uri="http://www.test.com/xmlns/test/9"/>
    <mulexml:namespace prefix="oa" uri="http://www.test.com/xmlns/test/foundation"/>
</mulexml:namespace-manager> 

然后使用以下 XPATH 检索值,就像我在记录器中所做的那样:-

<logger message="Value of id : #[xpath('//_ord:ProcessOrder/oa:ApplicationArea/oa:ID/text()').text]" level="INFO" doc:name="Logger"/>  

所以,这两种方式都可以精确计算价值,并且都可以完美运行

【讨论】:

  • 嗨,它可以在没有 cxf 组件的情况下正常工作,但如果我在其中使用 CXF 组件和 wsdl,这些 xpaths 无法正常工作,你可以为此提供任何解决方案。提前致谢
  • 但是在您的流程中没有提到 CXF 组件...所以解决方案按照给出的流程...您能否接受答案并关闭此问题...请打开一个新的在您的流程中与 CXF 组件有关的问题,我们将对其进行调查
【解决方案2】:

你有什么理由在这个表达式中需要xpath:

<expression-component doc:name="Expression">#[xpath('//*[local-name()="ID"]/text()')]</expression-component>

根据this page,不需要为local-name()函数加前缀。另外,我不确定你为什么把整个放到 CDATA 部分。

【讨论】:

  • 我在 c 数据中不需要它我用变量测试了它没有帮助所以我用 cdata 尝试了你能帮我吗
猜你喜欢
  • 2018-05-15
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多