[英]How to extract elements from xml having multiple namespaces using java smooks and freemarker
我正在嘗試使用Smooks進行XML-XML轉換。 輸入XML具有多個名稱空間時遇到問題。
這是我的示例輸入文件
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<look:Trans xmlns:look="http://www.example.com">
<Response>0</Response>
<DGX>
<SIGN>1</SIGN>
</DGX>
</look:Trans>
</soapenv:Body>
</soapenv:Envelope>
而我的配置文件
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="default.serialization.on">false</param>
</params>
<resource-config selector="Trans">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<ftl:freemarker applyOnElement="Trans">
<ftl:template>
<!-- <#ftl ns_prefixes={"look": "http://www.example.com"}> <#outputformat 'XML'>
<Response>
<Code>${look\:Trans.Response} </Code>
<Sign>${look\:Trans.DGX.SIGN} </Sign>
</Response> </#outputformat> -->
</ftl:template>
我該如何進行這項工作?
我不確定Smooks選擇哪個XML節點作為模板上下文根...如果是soapenv:Body
那么我在模板中看不到問題。 無論如何,我已經在http://try.freemarker.org/上嘗試過此操作,其中doc
是文檔節點(因此與Smooks有所不同),以證明多個名稱空間應像這樣工作:
模板:
<#ftl ns_prefixes={"look": "http://www.example.com", "soapenv": "http://schemas.xmlsoap.org/soap/envelope/"}
output_format="XML">
${doc.soapenv\:Envelope.soapenv\:Body.look\:Trans.DGX.SIGN}
數據模型:
doc=
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<look:Trans xmlns:look="http://www.example.com">
<Response>0</Response>
<DGX>
<SIGN>123</SIGN>
</DGX>
</look:Trans>
</soapenv:Body>
</soapenv:Envelope>
結果:
123
順便說一句,請注意,您可以僅將output_format='XML'
添加到<#ftl ...>
來代替<#outputformat 'XML'>...</#outputformat>
<#ftl ...>
。 更短
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.