【问题标题】:Xpath expression in mulemule 中的 Xpath 表达式
【发布时间】:2018-05-15 04:12:47
【问题描述】:

我需要调用一个提供以下 xml 响应的 rest api。我只想使用 xpath 提取字段“EntryID”。有人可以帮我解决 xpath 表达式以及在 http 出站后我需要使用什么转换器。我使用的是 Mule 3.5.0 版本。

输入 XML

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <entry>
       <author>
            <name>teden</name>
        </author>
        <contributor>
            <name>angelaw</name>
        </contributor>
        <content type="xhtml">
            <Entry>
                <EntryID>53339</EntryID>                
            </Entry>
        </content>
    </entry>
</feed>

骡流

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

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">
    <mulexml:namespace-manager
        includeConfigNamespaces="true">
        <mulexml:namespace prefix="v1"
            uri="http://www.w3.org/2005/Atom" />
    </mulexml:namespace-manager>
    <mulexml:xml-to-dom-transformer name="Parse_XML"
        doc:name="Parse XML Message" />
    <flow name="testFlow">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8081" path="test" doc:name="HTTP" />
        <transformer ref="Parse_XML" doc:name="Transformer Reference" />
        <expression-component doc:name="Expression"><![CDATA[flowVars.test= xpath('/v1:feed/entry/content/Entry/EntryID/text()').text;]]></expression-component>
        <logger level="INFO" doc:name="Logger" />
    </flow>
</mule>

TIA。

【问题讨论】:

    标签: xpath mule


    【解决方案1】:

    由于您使用的是 Mule 3.5 运行时,您可以使用以下方式获取结果

    全局放置 Mule 命名空间管理器:

    <mulexml:namespace-manager includeConfigNamespaces="true">
          <mulexml:namespace prefix="v1" uri="http://www.w3.org/2005/Atom"/>
     </mulexml:namespace-manager>
    

    确保为命名空间管理器添加命名空间:

    <mule xmlns="http://www.mulesoft.org/schema/mule/core"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
           xsi:schemaLocation="
               http://www.mulesoft.org/schema/mule/xml
               http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
               http://www.mulesoft.org/schema/mule/core
               http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
    

    参考:https://docs.mulesoft.com/mule-user-guide/v/3.5/xml-namespaces

    更新

    您可以在 MEL 中使用 XPATH 表达式提取数据:

     <logger message="#[xpath('/v1:feed/v1:entry/v1:content/v1:Entry/v1:EntryID/text()').text]" level="INFO" doc:name="Logger"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 2013-07-24
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多