我需要调用一个rest api,它给出了下面的xml响应。我想提取字段' EntryID'仅使用xpath。有人可以帮我解决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。
答案 0 :(得分: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"/>